gpt4 book ai didi

java - Android 致命异常 NullPointer

转载 作者:太空狗 更新时间:2023-10-29 16:42:49 25 4
gpt4 key购买 nike

应用程序崩溃,我收到此错误:

02-19 13:48:34.168: E/AndroidRuntime(1070): FATAL EXCEPTION: main
02-19 13:48:34.168: E/AndroidRuntime(1070): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prva/com.example.prva.MainMenu}: java.lang.NullPointerException

02-19 13:48:34.168: E/AndroidRuntime(1070): Caused by: java.lang.NullPointerException
02-19 13:48:34.168: E/AndroidRuntime(1070): at com.example.prva.MainMenu.onCreate(MainMenu.java:49)

当我尝试运行此 Activity 时:

public class MainMenu extends Activity{ 
Calendar c = Calendar.getInstance();
int currentsecond = c.get(Calendar.SECOND);
Handler h=new Handler();
DatabaseManager db;



@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
h.post(new Runnable(){
@Override
public void run() {
String currentDateTimeString = new SimpleDateFormat("dd.MM.yyyy HH:mm").format(new Date());
TextView text = (TextView) findViewById(R.id.txtDate);
text.setText(currentDateTimeString);
h.postDelayed(this, 60000);
}
});

db = new DatabaseManager(this);


Button btnalarm = (Button) findViewById(R.id.btnSetAlarm);
btnalarm.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainMenu.this, SetAlarm.class));
}
});

Button btnmelody = (Button)findViewById(R.id.btnSetMelody);
btnmelody.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainMenu.this, SetMelody.class));
}
});

Button btnlist = (Button) findViewById(R.id.btnAlarmList);
btnlist.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent1 = new Intent(MainMenu.this, AlarmList.class);
startActivity(intent1);
}
});

Button btndata = (Button) findViewById(R.id.btnDatabase);
btndata.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent2 = new Intent(MainMenu.this, ManageDatabase.class);
startActivity(intent2);
}
});
}
}

第 49 行是:

btnalarm.setOnClickListener(new View.OnClickListener() {

最佳答案

当你写这篇文章时:

    Button btnalarm = (Button) findViewById(R.id.btnSetAlarm);
btnalarm.setOnClickListener(new View.OnClickListener() {

您假设找到该 View 会返回一个非空按钮,但您错了。

查看搜索失败的原因,您就会得到答案。

关于java - Android 致命异常 NullPointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14959243/

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