gpt4 book ai didi

java - 在android中更改背景图像

转载 作者:行者123 更新时间:2023-11-30 03:50:10 25 4
gpt4 key购买 nike

每次单击按钮时,我都试图更改我的背景图像。问题是,我总是遇到 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 时。

最佳答案

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);
...
}

关于java - 在android中更改背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14281419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com