gpt4 book ai didi

android - 当更改 Activity 时,应用程序已停止

转载 作者:行者123 更新时间:2023-12-02 05:03:52 25 4
gpt4 key购买 nike

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

更改 Activity 时,应用程序已停止。我使用淡入和淡出。如何解决这个问题?

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

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,但它不起作用。

最佳答案

检查 Button goManageList 的 ID 是否存在于您的 R.layout.activity_main

因为你有这个错误

Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

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

关于android - 当更改 Activity 时,应用程序已停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45478592/

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