0

好的,所以我一直试图找到解决这个问题的几个小时,但我找不到。这是我日志猫所说的空指针期望的代码:

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>

所以当我尝试运行它时,它得到一个空指针异常,我不知道它有什么问题,有什么想法吗?

4

1 回答 1

3

您需要先setContentView致电findViewById

setContentView(R.layout.the_layout_xml_name);
begin=(Button) findViewById(R.id.button);
// ...
于 2012-05-17T23:14:08.933 回答