Java.lang.RuntimeException:无法启动活动

时间:2011-08-23 14:07:49

标签: android exception nullpointerexception

我的Logcat:

  

08-23 18:57:03.781:ERROR / AndroidRuntime(294):java.lang.RuntimeException:无法启动活动ComponentInfo {com.HelloApp / com.HelloApp.TestActivity}:java.lang.NullPointerException

任何人都可以告诉我,为什么我会收到此错误?

    public class TestActivity extends Activity 
    {
     public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setTitle("Test App");
    requestWindowFeature(Window.FEATURE_LEFT_ICON);
    setContentView(R.layout.main);
    initControls();
    }
    public void initControls()
    {
    Button btn = (Button) findViewById(R.id.close);
    //Button button=new Button(this);
    btn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) 
    {
    Intent myIntent = new Intent(view.getContext(),GetParameter.class);
    startActivity(myIntent);
    //startActivity( new Intent(null, GetParameter.class));
    }
    });
     }
  }

1 个答案:

答案 0 :(得分:0)

你的main.xml必须是这样的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">    
    <TextView android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"/> 
    <Button android:id="@+id/close" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="Test Button" /> 
</LinearLayout>

检查