每次点击一个按钮,我都会尝试更改背景图片。问题是,我总是得到一个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>
堆栈跟踪/ 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时。
答案 0 :(得分:1)
backBtn
的名称在您的java代码和xml声明中不匹配。
在 XML ,android:id="@+id/backBtn"
在 Java ,prevtbtn = (ImageView)findViewById(R.id.backbtn);
您应该使用:
public void switchPage(int ctr)
{
hLayout = (RelativeLayout)findViewById(R.id.helpLayout);
nextbtn = (ImageView)findViewById(R.id.nextBtn);
prevtbtn = (ImageView)findViewById(R.id.backBtn);
...
}
答案 1 :(得分:0)
您已经引用了 hLayout ,无需在switchPage函数中重新分配它。 请检查您是否在activity_help.xml中同时拥有ImageView(nextbtn,prevtbtn)。 另外你怎么称呼showNextPage&amp; showPrevPage和你在那里传递什么视图?
答案 2 :(得分:0)
尝试以下步骤:
int[] imageArr=[R.drawble.image1.....];
counter value=10
表示获取imageArr[10]
,现在将此值设置为背景。答案 3 :(得分:0)
检查您的代码和布局,名称之间存在差异:
android:id="@+id/backBtn"
prevtbtn = (ImageView)findViewById(R.id.backbtn);
我还建议您实现自己的OnClickListener并调试onClick()函数。