2013-01-19 74 views
1

我是新來的,並且是android。我查看了類似的主題,但仍未找到解決方案。Android Eclipse錯誤 - 不幸的是<project>已經停止

這是我的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代碼中任何東西,還它給我錯誤。

請幫助...

+0

你應該包括堆棧跟蹤,讓我們知道爲什麼應用程序停止。 – vault

+0

編輯..我認爲問題與聽衆和這個R.id有關,因爲當我沒有使用它,只是在另一個方法'onclicksubmit'上打印消息'Hello'時,它正在工作... – bookishq

+0

好的,我發現了這個問題。 – vault

回答

0

您不能在方法外進行分配。將這個裏面的onCreate:

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

這是寫在堆棧跟蹤:sub爲空當你試圖點擊監聽器分配給它。

此外,Glenn是正確的:如果您要手動設置點擊偵聽器,請刪除屬性。


EDITED。我以前的答覆:

按鈕的排列是錯誤的:

android:layout_below="@+id/checkBox4" 

checkBox4不存在。

+0

不,這不是錯誤。其實,我的代碼中有多個複選框。爲了簡短起見,我只在這裏寫了兩個,並犯了一個錯誤。 – bookishq

+0

如果你沒有發佈整個代碼,我們不能幫你。 – vault

+0

我編輯的代碼.. – bookishq

0

最好刪除屬性。

<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" /> 
+0

錯誤仍然存​​在... – bookishq

+0

你可以用報告的logcat錯誤更新你的問題嗎? – 2013-01-19 14:16:58

+0

嘗試刪除'tools:context =「。MainActivity「' – 2013-01-19 14:18:34

相關問題