--更新-- 2012 年 4 月 17 日
似乎只有在我添加时才会崩溃:
android:layout_above="@+id/lakeSearchAdWebView"
到lakeSearchScrollView,将它添加到lakeSearchDetailsWebView 工作正常。
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:id="@+id/lakeSearchScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:visibility="visible" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="0dp"
android:src="@drawable/contentbg" />
<EditText
android:id="@+id/searchBar"
android:layout_width="274dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="83dp" >
<requestFocus />
</EditText>
<Button
android:id="@+id/lakeSearchSearchBTN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/searchBar"
android:layout_below="@+id/searchBar"
android:layout_marginTop="15dp"
android:text="Search" />
<TableLayout
android:id="@+id/resultsTableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/lakeSearchSearchBTN" >
</TableLayout>
</RelativeLayout>
</ScrollView>
<WebView
android:id="@+id/lakeSearchDetailsWebView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:visibility="invisible" />
<WebView
android:id="@+id/lakeSearchAdWebView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>
我想设置lakeSearchScrollView 和lakeSearchDetailsWebView 以便它们始终位于lakeSearchAdWebview 上方,因此lakeSearchAdWebview 始终可见。一次只有一个lakeSearchScrollView 和lakeSearchDetailsWebView 可见,这就是它们被设置为彼此重叠的原因。
如果我将以下行添加到lakeSearchScrollView,应用程序崩溃:
android:layout_above="@+id/lakeSearchAdWebView"
谁能告诉我我做错了什么?我对 Android 还是很陌生,所以我认为这可能是新手的错误......提前谢谢!
--更新-- 源代码:
package com.theappdoor.ifish;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TabHost;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.util.Log;
public class iFishLakeSearch extends Activity {
private EditText searchBar;
private TableLayout resultsTableLayout;
private DataHelper dh;
private iFishLakeDetails iFishLD;
private Button lakeSearchBTN;
private ScrollView searchScrollView;
private WebView detailsWebView;
private String lake_id;
private WebView lakeSearchAdWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.lakesearch);
//This sets the context to the be of the parent, which is what we want
View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.lakesearch, null);
setContentView(viewToLoad);
//setup vars
this.dh = new DataHelper(this);
this.iFishLD = new iFishLakeDetails(this);
// connect our elements from the UI XML
this.searchBar = (EditText) findViewById(R.id.searchBar); //LINE 61
this.resultsTableLayout = (TableLayout) findViewById(R.id.resultsTableLayout);
this.lakeSearchBTN = (Button) findViewById(R.id.lakeSearchSearchBTN);
this.lakeSearchBTN.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
lakeSearch(v);
}
});
this.searchScrollView = (ScrollView) findViewById(R.id.lakeSearchScrollView);
this.detailsWebView = (WebView) findViewById(R.id.lakeSearchDetailsWebView);
WebSettings webSettings = this.detailsWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
this.detailsWebView.setWebViewClient(new MyWebViewClient());
this.detailsWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
this.detailsWebView.loadUrl("file:///android_asset/webassets/index.html");
this.lakeSearchAdWebView = (WebView)findViewById(R.id.lakeSearchAdWebView);
webSettings = this.lakeSearchAdWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
this.lakeSearchAdWebView.loadUrl(this.getString(R.string.server)+"/iFish_ads/");
}
第 61 行:
this.searchBar = (EditText) findViewById(R.id.searchBar);
- 更新 -
崩溃日志:
04-16 13:22:23.951: D/AndroidRuntime(20896): Shutting down VM
04-16 13:22:23.951: W/dalvikvm(20896): threadid=1: thread exiting with uncaught exception (group=0x40015578)
04-16 13:22:23.970: E/AndroidRuntime(20896): FATAL EXCEPTION: main
04-16 13:22:23.970: E/AndroidRuntime(20896): java.lang.IllegalStateException: Could not execute method of the activity
04-16 13:22:23.970: E/AndroidRuntime(20896): at android.view.View$1.onClick(View.java:2154)
04-16 13:22:23.970: E/AndroidRuntime(20896): at android.view.View.performClick(View.java:2538)
04-16 13:22:23.970: E/AndroidRuntime(20896): at android.view.View$PerformClick.run(View.java:9152)
04-16 13:22:23.970: E/AndroidRuntime(20896): at android.os.Handler.handleCallback(Handler.java:587)
04-16 13:22:23.970: E/AndroidRuntime(20896): at android.os.Handler.dispatchMessage(Handler.java:92)
04-16 13:22:23.970: E/AndroidRuntime(20896): at android.os.Looper.loop(Looper.java:123)
04-16 13:22:23.970: E/AndroidRuntime(20896): at android.app.ActivityThread.main(ActivityThread.java:3687)
04-16 13:22:23.970: E/AndroidRuntime(20896): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 13:22:23.970: E/AndroidRuntime(20896): at java.lang.reflect.Method.invoke(Method.java:507)
04-16 13:22:23.970: E/AndroidRuntime(20896): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
04-16 13:22:23.970: E/AndroidRuntime(20896): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-16 13:22:23.970: E/AndroidRuntime(20896): at dalvik.system.NativeStart.main(Native Method)
04-16 13:22:23.970: E/AndroidRuntime(20896): Caused by: java.lang.reflect.InvocationTargetException
04-16 13:22:23.970: E/AndroidRuntime(20896): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 13:22:23.970: E/AndroidRuntime(20896): at java.lang.reflect.Method.invoke(Method.java:507)
04-16 13:22:23.970: E/AndroidRuntime(20896): at android.view.View$1.onClick(View.java:2149)
04-16 13:22:23.970: E/AndroidRuntime(20896): ... 11 more
04-16 13:22:23.970: E/AndroidRuntime(20896): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.theappdoor.ifish/com.theappdoor.ifish.TabGroup2Activity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.theappdoor.ifish/com.theappdoor.ifish.iFishLakeSearch}: java.lang.ClassCastException: android.webkit.WebView
04-16 13:22:23.970: E/AndroidRuntime(20896): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
04-16 13:22:23.970: E/AndroidRuntime(20896): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1491)
04-16 13:22:23.970: E/AndroidRuntime(20896): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
04-16 13:22:23.970: E/AndroidRuntime(20896): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
04-16 13:22:23.970: E/AndroidRuntime(20896): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:657)
04-16 13:22:23.970: E/AndroidRuntime(20896): at android.widget.TabHost.setCurrentTab(TabHost.java:329)
04-16 13:22:23.970: E/AndroidRuntime(20896): at com.theappdoor.ifish.iFishHome.goToLakeSearch(iFishHome.java:20)
04-16 13:22:23.970: E/AndroidRuntime(20896): ... 14 more
04-16 13:22:23.970: E/AndroidRuntime(20896): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.theappdoor.ifish/com.theappdoor.ifish.iFishLakeSearch}: java.lang.ClassCastException: android.webkit.WebView
04-16 13:22:23.970: E/AndroidRuntime(20896): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
04-16 13:22:23.970: E/AndroidRuntime(20896): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1491)
04-16 13:22:23.970: E/AndroidRuntime(20896): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
04-16 13:22:23.970: E/AndroidRuntime(20896): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
04-16 13:22:23.970: E/AndroidRuntime(20896): at com.theappdoor.ifish.TabGroupActivity.startChildActivity(TabGroupActivity.java:116)
04-16 13:22:23.970: E/AndroidRuntime(20896): at com.theappdoor.ifish.TabGroup2Activity.onCreate(TabGroup2Activity.java:10)
04-16 13:22:23.970: E/AndroidRuntime(20896): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-16 13:22:23.970: E/AndroidRuntime(20896): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
04-16 13:22:23.970: E/AndroidRuntime(20896): ... 20 more
04-16 13:22:23.970: E/AndroidRuntime(20896): Caused by: java.lang.ClassCastException: android.webkit.WebView
04-16 13:22:23.970: E/AndroidRuntime(20896): at com.theappdoor.ifish.iFishLakeSearch.onCreate(iFishLakeSearch.java:61)
04-16 13:22:23.970: E/AndroidRuntime(20896): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-16 13:22:23.970: E/AndroidRuntime(20896): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
04-16 13:22:23.970: E/AndroidRuntime(20896): ... 27 more
04-16 13:22:44.244: I/Process(20896): Sending signal. PID: 20896 SIG: 9