【发布时间】: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