我无法弄清楚为什么这两个按钮不起作用,我有一个布局文件,当用户在我的应用程序中选择某个主题时会在其中创建。有了布局,它有一个 web 视图,它有 2 个按钮可以后退和前进。
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:text="Back"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:background="@drawable/button_blue"
style="@style/ButtonText">
</Button>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/button_blue"
style="@style/ButtonText"
android:text="Forward">
</Button>
<WebView
android:id="@+id/webview01"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1.12" >
这不是整个布局文件,但这是我正在为 webview 和 2 个按钮使用的一些东西,现在在我的主要活动中是我的按钮代码。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Prefs.theme.equals("Theme1"))
setContentView(R.layout.main);
else if (Prefs.theme.equals("Theme2"))
setContentView(R.layout.main2);
else if (Prefs.theme.equals("Theme3"))
setContentView(R.layout.main3);
else setContentView(R.layout.main);
btnForward=(Button) findViewById (R.id.button1);
btnBackward=(Button) findViewById (R.id.button2);
btnForward.setOnClickListener(this);
btnBackward.setOnClickListener(this);
// more code within on create .....
// Later in the code
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
WebViewClientDemoActivity.web.goBack();
break;
case R.id.button2:
WebViewClientDemoActivity.web.goForward();
break;
}
//
我的问题是默认情况下它加载 main.xml 而不是 main3(这是 2 个按钮)
LogCat 错误
07-27 16:00:34.802: E/AndroidRuntime(547): FATAL EXCEPTION: main
07-27 16:00:34.802: E/AndroidRuntime(547): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jaisonbrooks.enlighten/com.jaisonbrooks.enlighten.WebViewClientDemoActivity}: java.lang.NullPointerException
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.os.Looper.loop(Looper.java:123)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-27 16:00:34.802: E/AndroidRuntime(547): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 16:00:34.802: E/AndroidRuntime(547): at java.lang.reflect.Method.invoke(Method.java:507)
07-27 16:00:34.802: E/AndroidRuntime(547): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-27 16:00:34.802: E/AndroidRuntime(547): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-27 16:00:34.802: E/AndroidRuntime(547): at dalvik.system.NativeStart.main(Native Method)
07-27 16:00:34.802: E/AndroidRuntime(547): Caused by: java.lang.NullPointerException
07-27 16:00:34.802: E/AndroidRuntime(547): at com.jaisonbrooks.enlighten.WebViewClientDemoActivity.onCreate(WebViewClientDemoActivity.java:75)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-27 16:00:34.802: E/AndroidRuntime(547): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-27 16:00:34.802: E/AndroidRuntime(547): ... 11 more