gpt4 book ai didi

java - 无法实例化 Activity ComponentInfo{com.example.ragefacefolks/com.example.ragefacefolks.MainActivity} : java. lang.NullPointerException

转载 作者:行者123 更新时间:2023-11-29 05:37:09 25 4
gpt4 key购买 nike

今天是我第一天接触java。我无法弄清楚这个错误的原因。

MainActivity.java如下所示

package com.example.ragefacefolks;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.app.Activity;

public class MainActivity extends Activity {

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

Button bMain = (Button) findViewById(R.id.button1);

@Override
public void registerForContextMenu(View view) {
// TODO Auto-generated method stub
super.registerForContextMenu(bMain);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);

}


}

你们不难知道它的原因。需要一点帮助。log cat 正在显示这些结果

09-27 07:59:24.677: D/AndroidRuntime(555): Shutting down VM
09-27 07:59:24.677: W/dalvikvm(555): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-27 07:59:24.707: E/AndroidRuntime(555): FATAL EXCEPTION: main
09-27 07:59:24.707: E/AndroidRuntime(555): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ragefacefolks/com.example.ragefacefolks.MainActivity}: java.lang.NullPointerException
09-27 07:59:24.707: E/AndroidRuntime(555): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
09-27 07:59:24.707: E/AndroidRuntime(555): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-27 07:59:24.707: E/AndroidRuntime(555): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-27 07:59:24.707: E/AndroidRuntime(555): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-27 07:59:24.707: E/AndroidRuntime(555): at android.os.Handler.dispatchMessage(Handler.java:99)
09-27 07:59:24.707: E/AndroidRuntime(555): at android.os.Looper.loop(Looper.java:123)
09-27 07:59:24.707: E/AndroidRuntime(555): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-27 07:59:24.707: E/AndroidRuntime(555): at java.lang.reflect.Method.invokeNative(Native Method)

最佳答案

这个

Button bMain = (Button) findViewById(R.id.button1);

应该在 setContentView 之后的 onCreate 中。你在任何方法之外初始化它

private Button bMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bMain = (Button) findViewById(R.id.button1);
}

关于java - 无法实例化 Activity ComponentInfo{com.example.ragefacefolks/com.example.ragefacefolks.MainActivity} : java. lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19077812/

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