当我使用许多按钮点击时,Android应用会崩溃

时间:2015-05-25 20:35:34

标签: android button nullpointerexception onclicklistener

public class App2Activity extends Activity  implements OnClickListener {

    Button button;
    Button button2;
    Button button3;
    Button button4;
    Button button5;

    @Override
    public void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        button=(Button) findViewById(R.id.button1);
        button2=(Button) findViewById(R.id.button2);
        button3=(Button) findViewById(R.id.button3);
        button4=(Button) findViewById(R.id.button4);
        button5=(Button) findViewById(R.id.button5);
        button.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);    
    }//End oncreate      

    @Override
    public void onClick(View v){

        switch(v.getId()){

        case R.id.button1:
            Intent intent=new Intent(this,MainActivity.class);
            this.startActivity(intent);
            break;

        case R.id.button2:
            Intent intent2=new Intent(this,PollaplasiasmosActivity.class);
            this.startActivity(intent2);
            break;


        case R.id.button3:
            Intent intent3=new Intent(this,AfairesiActivity.class);
            this.startActivity(intent3);
            break;


        case R.id.button4:
            Intent intent4=new Intent(this,MaxActivity.class);
            this.startActivity(intent4);
            break;

        case R.id.button5:
            Intent intent5=new Intent(this,MinActivity.class);
            this.startActivity(intent5);
            break;  

        }
    }
}

Logcat如下

05-25 23:11:45.878 8451 8451 D     skia            jpeg_decoder mode 1, config 6, w 1667, h 1667, sample 1, bsLength 9ff28!!
05-25 23:20:31.254 11279 11279 D   skia            jpeg_decoder mode 1, config 6, w 1667, h 1667, sample 1, bsLength 9ff28!!
05-25 23:20:31.748 11279 11279 E   AndroidRuntime  FATAL EXCEPTION: main
05-25 23:20:31.748 11279 11279 E   AndroidRuntime  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.myapp3/com.mycompany.myapp3.App2Activity}: java.lang.NullPointerException
05-25 23:20:31.748 11279 11279 E   AndroidRuntime  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2372)
05-25 23:20:31.748 11279 11279 E   AndroidRuntime  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)
05-25 23:20:31.748 11279 11279 E   AndroidRuntime  at android.app.ActivityThread.access$600(ActivityThread.java:162)
05-25 23:20:31.748 11279 11279 E   AndroidRuntime  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
05-25 23:20:31.748 11279 11279 E   AndroidRuntime  at android.os.Handler.dispatchMessage(Handler.java:107)
05-25 23:20:31.748 11279 11279 E   AndroidRuntime  at android.os.Looper.loop(Looper.java:194)
05-25 23:20:31.748 11279 11279 E   AndroidRuntime  at android.app.ActivityThread.main(ActivityThread.java:5400)
05-25 23:20:31.748 11279 11279 E   AndroidRuntime  at java.lang.reflect.Method.invokeNative(Native Method)
05-25 23:20:31.748 11279 11279 E   AndroidRuntime  at java.lang.reflect.Method.invoke(Method.java:525)
05-25 23:20:31.748 11279 11279 E   AndroidRuntime  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:837)
05-25 23:20:31.748 11279 11279 E   AndroidRuntime  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
05-25 23:20:31.748 11279 11279 E   AndroidRuntime  at dalvik.system.NativeStart.main(Native Method)
05-25 23:20:31.748 11279 11279 E   AndroidRuntime  Caused by: java.lang.NullPointerException
05-25 23:20:31.748 11279 11279 E   AndroidRuntime  at com.mycompany.myapp3.App2Activity.onCreate(App2Activity.java:63)
05-25 23:20:31.748 11279 11279 E   AndroidRuntime  at android.app.Activity.performCreate(Activity.java:5122)
05-25 23:20:31.748 11279 11279 E   AndroidRuntime  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
05-25 23:20:31.748 11279 11279 E   AndroidRuntime  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2336)
05-25 23:20:31.748 11279 11279 E   AndroidRuntime  ... 11 more

代码的第一部分是主Activity,其中我有按钮,可以导致其他活动。代码的第二部分是Logcat。关键是我的应用程序没有错误但它崩溃了。你可以推荐我任何解决方案因为我尝试了很多其他选择。感谢。

在main2.xml中,我根据您的观察修复了代码

    <Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/button3"
    android:drawableTop="@drawable/cat"
    android:text=""
    android:color="#66CCFF" />

1 个答案:

答案 0 :(得分:0)

您将获得第63行的NPE

"{Binding BgBrush}"

您的按钮未分配给对象参考 -

Caused by: java.lang.NullPointerException
at com.mycompany.myapp3.App2Activity.onCreate(App2Activity.java:63)

返回null;