【问题标题】:Java.lang.RuntimeException: Unable to start activityJava.lang.RuntimeException:无法启动活动
【发布时间】:2011-08-23 14:07:49
【问题描述】:

我的日志猫:

08-23 18:57:03.781: 错误/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));
    }
    });
     }
  }

【问题讨论】:

  • 是的,你有一个空指针异常。如果您希望我们真正提供帮助,请向我们展示相关代码。
  • 显示您的 TestActivity 类的代码......
  • 如前所述,我们需要更多代码来真正帮助诊断问题,但您的变量之一为空。不幸的是,这是最常见的错误之一,很难诊断。查找指向确切行号的错误行,并将其缩小到可以快速解决问题的位置。
  • 你能告诉我们main.xml吗?
  • @Soniya 您在清单文件中添加了活动名称吗?我可能错了,但请检查一下。

标签: android exception nullpointerexception


【解决方案1】:

你的 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>

检查一下

【讨论】:

    猜你喜欢
    • 2017-09-19
    • 2014-05-12
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多