我试图每次单击按钮时更改背景图像。问题是,我总是得到一个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>
Stacktrace / logcat:
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时。
最佳答案
检查您的代码和布局名称之间是否存在差异:
android:id="@+id/backBtn"
prevtbtn = (ImageView)findViewById(R.id.backbtn);
我也建议实现自己的OnClickListener并调试onClick()函数。