[英]cant figure out null pointer exception
好吧,所以我已經嘗試了一個小時的解決方案,但是我找不到。 這是日志貓說空指針期望值的代碼:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class splash extends Activity{
Button begin;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
begin=(Button) findViewById(R.id.button);
begin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent startmenu=new Intent("com.life.project.menu");
startActivity(startmenu);
}
});
}
}
這是xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:background="@drawable/icon3">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="You have 20 minutes to increase your status as much as possible." />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Begin!"
android:layout_gravity="center"
android:typeface="monospace" />
</LinearLayout>
所以當我嘗試運行它時,它會得到一個空指針異常,並且我不知道它出了什么問題,有什么想法嗎?
您需要在findViewById
之前調用setContentView
:
setContentView(R.layout.the_layout_xml_name);
begin=(Button) findViewById(R.id.button);
// ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.