Android Eclipse错误 - 不幸的是<project>已经停止</project>

时间:2013-01-19 13:40:02

标签: android

我是新来的和android。我查看了类似的主题,但仍未找到解决方案。

这是我的xml代码.. 我想要做的是,点击提交按钮,没有单击复选框,然后产生错误或成功消息。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:ems="10"
    android:hint="Enter Your Name" >

    <requestFocus />
  </EditText>



<CheckBox
    android:id="@+id/checkBox3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/checkBox1"
    android:layout_below="@+id/checkBox2"
    android:layout_marginLeft="15dp"
    android:text="Hello" />

<CheckBox
    android:id="@+id/checkBox4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/checkBox3"
    android:layout_below="@+id/checkBox3"
    android:text="Noon" />

<CheckBox
    android:id="@+id/checkBox2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/checkBox3"
    android:layout_below="@+id/checkBox1"
    android:text="Evening" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/radioGroup2"
    android:layout_centerVertical="true"
    android:layout_marginLeft="20dp"
    android:checked="true"
    android:text="Morning" />

<Button
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/checkBox1"
    android:layout_below="@+id/checkBox4"
    android:onClick="onClickSubmit"
    android:text="Hello" />

这是Java代码......

CheckBox c1=(CheckBox) findViewById(R.id.checkBox1);
Button sub=(Button) findViewById(R.id.submit);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



sub.setOnClickListener(new View.OnClickListener() {  
    public void onClick(View v) {  

                    Toast.makeText(MainActivity.this, "Submit!", Toast.LENGTH_SHORT).show();  
            }  
});  

错误记录...

 01-19 14:13:47.102: E/AndroidRuntime(2554): FATAL EXCEPTION: main
   01-19 14:13:47.102: E/AndroidRuntime(2554): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.finalfortoday/com.example.finalfortoday.MainActivity}: java.lang.NullPointerException
01-19 14:13:47.102: E/AndroidRuntime(2554):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at android.os.Looper.loop(Looper.java:137)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at java.lang.reflect.Method.invokeNative(Native Method)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at java.lang.reflect.Method.invoke(Method.java:511)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at dalvik.system.NativeStart.main(Native Method)
01-19 14:13:47.102: E/AndroidRuntime(2554): Caused by: java.lang.NullPointerException
01-19 14:13:47.102: E/AndroidRuntime(2554):     at android.app.Activity.findViewById(Activity.java:1839)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at com.example.finalfortoday.MainActivity.<init>(MainActivity.java:16)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at java.lang.Class.newInstanceImpl(Native Method)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at java.lang.Class.newInstance(Class.java:1319)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
01-19 14:13:47.102: E/AndroidRuntime(2554):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
01-19 14:13:47.102: E/AndroidRuntime(2554):     ... 11 more

我还没有在Java代码中使用任何东西,但它仍然给我错误。

请帮助......

2 个答案:

答案 0 :(得分:0)

您无法在方法之外进行分配。把它放在onCreate:

CheckBox c1  = (CheckBox) findViewById(R.id.checkBox1);
Button sub = (Button) findViewById(R.id.submit);

它写在堆栈跟踪中:当您尝试为其分配单击侦听器时,sub为null。

此外,Glenn是对的:如果您要手动设置点击监听器,请删除android:onClick属性。


编辑。我以前的答案:

按钮对齐错误:

android:layout_below="@+id/checkBox4"

checkBox4不存在。

答案 1 :(得分:0)

最好删除android:onClick属性。

<Button
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/checkBox1"
    android:layout_below="@+id/checkBox4"
    android:onClick="onClickSubmit"
    android:text="Hello" />

应该是

<Button
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/checkBox1"
    android:layout_below="@+id/checkBox4"
    android:text="Hello" />