更改活动时,应用程序已停止

时间:2017-08-03 08:18:23

标签: android android-activity crash

package com.example.dh.qrock;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class mainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button goManageList = (Button)findViewById(R.id.goManageList);

        goManageList.setOnClickListener(
                new Button.OnClickListener()
                {
                    public void onClick(View v)
                    {
                        Intent myIntent = new Intent(v.getContext(), managelistActivity.class);
                        startActivity(myIntent);
                        finish();

                        overridePendingTransition(R.xml.madefadein, R.xml.splashfadeout);
                    }
                }
        );
    }
}

更改活动时,应用程序已停止。 我使用淡入淡出。 如何解决?

如果您需要其他代码,请在此处发表评论。

D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.dh.qrock, PID: 4356
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dh.qrock/com.example.dh.qrock.managelistActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                      at com.example.dh.qrock.managelistActivity.onCreate(managelistActivity.java:19)
                      at android.app.Activity.performCreate(Activity.java:6679)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6119) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)  

这是日志。 它在按钮中出现NullPointException。 如何修复NullPointException? 我将Button.setOnclicklistener更改为View.onclickListener,但它不起作用。

4 个答案:

答案 0 :(得分:0)

这是因为 overridePendingTransition(R.xml.madefadein,R.xml.splashfadeout);

答案 1 :(得分:0)

问题是你在意图中使用按钮的上下文,如下所示:

Intent myIntent = new Intent(v.getContext(), managelistActivity.class);

像这样给出当前活动的上下文:

Intent myIntent = new Intent(mainActivity.this, managelistActivity.class);

答案 2 :(得分:0)

将Button.OnClickListener更改为View.OnClickListener

 goManageList.setOnClickListener(
            new Button.OnClickListener()

   goManageList.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });

答案 3 :(得分:0)

检查Button goManageList

中是否存在R.layout.activity_main的ID

因为您有此错误

  

尝试调用虚方法'void   android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)”   在空对象引用上

这意味着您将setOnClickListener设置为空对象