每次单击按钮时,我都会尝试更改背景图像。问题是,我总是得到一个 javanullexception。
这是我的代码:
public class Help extends Activity {
int pageCtr = 0;
RelativeLayout hLayout;
ImageView nextbtn, prevtbtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_help);
hLayout = (RelativeLayout)findViewById(R.id.helpLayout);
hLayout.setBackgroundResource(R.drawable.helpbg1);
}
public void showNextPage(View v)
{
pageCtr++;
switchPage(pageCtr);
}
public void showPrevPage(View v)
{
pageCtr--;
switchPage(pageCtr);
}
public void switchPage(int ctr)
{
hLayout = (RelativeLayout)findViewById(R.id.helpLayout);
nextbtn = (ImageView)findViewById(R.id.nextBtn);
prevtbtn = (ImageView)findViewById(R.id.backbtn);
switch (ctr)
{
case 0:
prevtbtn.setVisibility(View.INVISIBLE);
hLayout.setBackgroundResource(R.drawable.helpbg1);
break;
case 1:
prevtbtn.setVisibility(View.VISIBLE);
hLayout.setBackgroundResource(R.drawable.helpbg2);
break;
case 2:
hLayout.setBackgroundResource(R.drawable.helpbg3);
break;
case 3:
nextbtn.setVisibility(View.VISIBLE);
hLayout.setBackgroundResource(R.drawable.helpbg4);
break;
case 4:
nextbtn.setVisibility(View.INVISIBLE);
hLayout.setBackgroundResource(R.drawable.helpbg5);
break;
}
}
}
我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/helpLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/backBtn"
android:layout_width="36dp"
android:layout_height="36dp"
android:src="@drawable/backbtn"
android:gravity="left"
android:layout_marginTop="150dp"
android:onClick="showPrevPage"
android:visibility="gone"/>
<ImageView
android:id="@+id/nextBtn"
android:layout_width="36dp"
android:layout_height="36dp"
android:src="@drawable/nextbtn"
android:layout_alignParentRight="true"
android:layout_marginTop="150dp"
android:onClick="showNextPage"/>
</RelativeLayout>
堆栈跟踪/日志猫:
01-11 23:36:28.476: E/AndroidRuntime(1351): FATAL EXCEPTION: main
01-11 23:36:28.476: E/AndroidRuntime(1351): java.lang.IllegalStateException: Could not execute method of the activity
01-11 23:36:28.476: E/AndroidRuntime(1351): at android.view.View$1.onClick(View.java:3591)
01-11 23:36:28.476: E/AndroidRuntime(1351): at android.view.View.performClick(View.java:4084)
01-11 23:36:28.476: E/AndroidRuntime(1351): at android.view.View$PerformClick.run(View.java:16966)
01-11 23:36:28.476: E/AndroidRuntime(1351): at android.os.Handler.handleCallback(Handler.java:615)
01-11 23:36:28.476: E/AndroidRuntime(1351): at android.os.Handler.dispatchMessage(Handler.java:92)
01-11 23:36:28.476: E/AndroidRuntime(1351): at android.os.Looper.loop(Looper.java:137)
01-11 23:36:28.476: E/AndroidRuntime(1351): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-11 23:36:28.476: E/AndroidRuntime(1351): at java.lang.reflect.Method.invokeNative(Native Method)
01-11 23:36:28.476: E/AndroidRuntime(1351): at java.lang.reflect.Method.invoke(Method.java:511)
01-11 23:36:28.476: E/AndroidRuntime(1351): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-11 23:36:28.476: E/AndroidRuntime(1351): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-11 23:36:28.476: E/AndroidRuntime(1351): at dalvik.system.NativeStart.main(Native Method)
01-11 23:36:28.476: E/AndroidRuntime(1351): Caused by: java.lang.reflect.InvocationTargetException
01-11 23:36:28.476: E/AndroidRuntime(1351): at java.lang.reflect.Method.invokeNative(Native Method)
01-11 23:36:28.476: E/AndroidRuntime(1351): at java.lang.reflect.Method.invoke(Method.java:511)
01-11 23:36:28.476: E/AndroidRuntime(1351): at android.view.View$1.onClick(View.java:3586)
01-11 23:36:28.476: E/AndroidRuntime(1351): ... 11 more
01-11 23:36:28.476: E/AndroidRuntime(1351): Caused by: java.lang.NullPointerException
01-11 23:36:28.476: E/AndroidRuntime(1351): at com.example.gems.Help.switchPage(Help.java:53)
01-11 23:36:28.476: E/AndroidRuntime(1351): at com.example.gems.Help.showNextPage(Help.java:31)
01-11 23:36:28.476: E/AndroidRuntime(1351): ... 14 more
这是一个简单的代码,但错误指向我的 pageCtr。似乎它没有看到我的 pageCtr 的任何值。试图将全局变量传递给希望它能够工作的方法,但它没有。错误是当我尝试增加或减少 pageCtr 时。