我有4个图像作为按钮,当选择正确的按钮时,会出现一个工作正常的箭头按钮。
我的问题是,我试图更改每个按钮的后台资源以在单击此箭头时进行更改,但我在该行得到一个空指针异常:
happybutton.setBackgroundResource(R.drawable.mum1);
我已经在java onCreate中声明了nextArrow按钮-
nextArrow = (Button) findViewById(R.id.nextArrow);
nextArrow.setOnClickListener(onClickListener);
类别:
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expressions);
Button happybutton = (Button) findViewById(R.id.happybutton);
happybutton.setOnClickListener(onClickListener);
Button sadbutton = (Button)findViewById(R.id.sadbutton);
sadbutton.setOnClickListener(onClickListener);
Button worriedbutton = (Button) findViewById(R.id.worriedbutton);
worriedbutton.setOnClickListener(onClickListener);
Button excitedbutton = (Button) findViewById(R.id.excitedbutton);
excitedbutton.setOnClickListener(onClickListener);
//Button nextArrow = (Button) findViewById(R.id.nextArrow);
//nextArrow.setOnClickListener(onClickListener);
tick = (ImageView) findViewById(R.id.tick);
cross = (ImageView)findViewById(R.id.cross);
cross2 = (ImageView) findViewById(R.id.cross2);
cross3 = (ImageView) findViewById(R.id.cross3);
nextArrow = (Button) findViewById(R.id.nextArrow);
nextArrow.setOnClickListener(onClickListener);
tryagain = MediaPlayer.create(Expressions.this,R.raw.tryagain);
correct = MediaPlayer.create(Expressions.this,R.raw.correct);
}
private OnClickListener onClickListener = new OnClickListener(){
public void onClick(final View view){
switch(view.getId()){
case R.id.happybutton:
tick.setVisibility(View.VISIBLE);
nextArrow.setVisibility(View.VISIBLE);
correct.start();
break;
case R.id.sadbutton:
cross.setVisibility(View.VISIBLE);
tryagain.start();
break;
case R.id.worriedbutton:
cross3.setVisibility(View.VISIBLE);
tryagain.start();
break;
case R.id.excitedbutton:
cross2.setVisibility(View.VISIBLE);
tryagain.start();
break;
case R.id.nextArrow:
happybutton.setBackgroundResource(R.drawable.mum1);
worriedbutton.setBackgroundResource(R.drawable.mum2);
sadbutton.setBackgroundResource(R.drawable.mum3);
excitedbutton.setBackgroundResource(R.drawable.mum4);
break;
}
}
日志:
07-31 16:17:11.312: E/AndroidRuntime(7818): FATAL EXCEPTION: main
07-31 16:17:11.312: E/AndroidRuntime(7818): java.lang.NullPointerException
07-31 16:17:11.312: E/AndroidRuntime(7818): at
com.example.Autism_App.Expressions$1.onClick(Expressions.java:84)
07-31 16:17:11.312: E/AndroidRuntime(7818): at android.view.View.performClick(View.java:4383)
07-31 16:17:11.312: E/AndroidRuntime(7818): at android.view.View$PerformClick.run(View.java:18097)
07-31 16:17:11.312: E/AndroidRuntime(7818): at android.os.Handler.handleCallback(Handler.java:725)
07-31 16:17:11.312: E/AndroidRuntime(7818): at android.os.Handler.dispatchMessage(Handler.java:92)
07-31 16:17:11.312: E/AndroidRuntime(7818): at android.os.Looper.loop(Looper.java:176)
07-31 16:17:11.312: E/AndroidRuntime(7818): at android.app.ActivityThread.main(ActivityThread.java:5279)
07-31 16:17:11.312: E/AndroidRuntime(7818): at java.lang.reflect.Method.invokeNative(Native Method)
07-31 16:17:11.312: E/AndroidRuntime(7818): at java.lang.reflect.Method.invoke(Method.java:511)
07-31 16:17:11.312: E/AndroidRuntime(7818): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-31 16:17:11.312: E/AndroidRuntime(7818): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-31 16:17:11.312: E/AndroidRuntime(7818): at dalvik.system.NativeStart.main(Native Method)
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/question"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:text="Which face is the happy face?"
android:textSize="80sp" />
<Button
android:id="@+id/happybutton"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignLeft="@+id/excitedbutton"
android:layout_alignParentBottom="true"
android:layout_marginBottom="43dp"
android:background="@drawable/happyface"/>
<Button
android:id="@+id/nextArrow"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="65dp"
android:visibility="invisible"
android:background="@drawable/nextarrow" />
<Button
android:id="@+id/worriedbutton"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignBaseline="@+id/happybutton"
android:layout_alignBottom="@+id/happybutton"
android:layout_alignLeft="@+id/sadbutton"
android:background="@drawable/worriedface" />
<Button
android:id="@+id/sadbutton"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_below="@+id/question"
android:layout_marginRight="113dp"
android:layout_marginTop="29dp"
android:layout_toLeftOf="@+id/nextArrow"
android:background="@drawable/sadface" />
<Button
android:id="@+id/excitedbutton"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignBaseline="@+id/sadbutton"
android:layout_alignBottom="@+id/sadbutton"
android:layout_alignLeft="@+id/question"
android:layout_marginLeft="116dp"
android:background="@drawable/excitedface" />
<ImageView
android:id="@+id/cross2"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignLeft="@+id/excitedbutton"
android:layout_alignTop="@+id/excitedbutton"
android:src="@drawable/cross"
android:visibility="invisible" />
<ImageView
android:id="@+id/cross"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignLeft="@+id/sadbutton"
android:layout_alignTop="@+id/sadbutton"
android:src="@drawable/cross"
android:visibility="invisible" />
<ImageView
android:id="@+id/tick"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignLeft="@+id/excitedbutton"
android:layout_alignTop="@+id/worriedbutton"
android:src="@drawable/tick"
android:visibility="invisible" />
<ImageView
android:id="@+id/cross3"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignLeft="@+id/worriedbutton"
android:layout_alignTop="@+id/worriedbutton"
android:src="@drawable/cross"
android:visibility="invisible" />
</RelativeLayout>
我错过了什么明显的东西吗?
按钮应该是类变量,如下所示:
public class MyClass {
Button happybutton;
Button sadbutton;
Button worriedbutton;
Button excitedbutton;
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expressions);
happybutton = (Button) findViewById(R.id.happybutton);
happybutton.setOnClickListener(onClickListener);
sadbutton = (Button)findViewById(R.id.sadbutton);
sadbutton.setOnClickListener(onClickListener);
worriedbutton = (Button) findViewById(R.id.worriedbutton);
worriedbutton.setOnClickListener(onClickListener);
excitedbutton = (Button) findViewById(R.id.excitedbutton);
excitedbutton.setOnClickListener(onClickListener);
...
我相信memeber<code>happybutton
您正在初始化一个局部变量,而不是实例字段
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expressions);
Button happybutton = (Button) findViewById(R.id.happybutton);
happybutton.setOnClickListener(onClickListener);
当您说< code>Button happybutton时,您正在创建一个新变量,它的作用域仅限于onCreate()方法,并且将在该方法的持续时间内取代该名称的实例字段。您初始化这个变量,因此调用它的setOnClickListener方法就可以了。但是变量在onCreate()方法的末尾消失了,其他方法只看到同名的永不初始化的实例字段。
相反,只需初始化您可能已经在onCreate()之外声明的实例字段
Button happybutton;
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expressions);
happybutton = (Button) findViewById(R.id.happybutton);
happybutton.setOnClickListener(onClickListener);
这可能是一个相当微妙的错误,因为用局部变量替换实例字段是完全有效的代码,不会触发任何编译器投诉。
我看到下面的错误日志从崩溃报告谷歌播放。这是发生在点击按钮。我不知道是什么触发了这个问题。请让我知道,如果有任何一个面临类似的问题。谢谢
以下是在sun.reflect.nativeMethodAccessorImpl.Invoke0(本机方法)在sun.reflect.nativeMethodAccessorImpl.Invoke(未知源)在sun.reflect.NativeMethodAccessorImpl.Invoke(未知源)在java.lang.Reflect.Method.Invoke(未知源)在com.codena
问题内容: 给了我一个 空指针异常 。btnEditor通过以下方式是XML Button的早期连接器: 在我的main.xml文件中: 真的,我不知道该怎么办… 解决 我忘记了我有两个文件: / res /布局 / res / layout-large 其中一个(大目录中)里面没有按钮,因此在使用大布局的设备上运行应用程序时出现错误。 问题答案: 最有可能你已经不叫用,这是要不你不叫 之前 这条
> 单击 受保护得空onPrepareDialog(int id,Dialog Dialog) 受保护的对话框onCreateDialog(int id) 如果我对timepickerdialog代码做了什么错误,请告诉我。
我正在使用贝宝快捷结账按钮。问题是,如果用户更改付款金额,paypal按钮将再次呈现。当我点击paypal按钮时,有没有办法渲染该按钮?我与贝宝结帐工作。js https://developer.paypal.com/docs/archive/checkout/integrate/# 更新:在web组件中工作,所以所有代码都在renderedcall back()中
问题内容: 此代码导致空指针异常。我不知道为什么: 我已经在调试器中检查了它,所有局部变量都不为空。怎么会这样呢?BiMap来自Google Collections。 问题答案: 空指针异常是将的结果拆箱的结果。如果不包含键,则返回“ of type” 。假设分配是给引用的,Java将值拆箱到中,导致空指针异常。 您应该检查或用作局部变量类型,以免取消装箱并采取相应措施。适当的机制取决于您的上下文