2014-01-05 15 views
2

我學習java通過視頻教程,但我有一個奇怪的問題,我創建了一個名爲OpenedClass在Java類,並實現了兩個班。
一個是OnClickListener和2是OnCheckChangeListener,但是當我在模擬器中運行該應用程序它總是給我的錯誤主叫OnClickListener
我的Java類的代碼是:java.lang.Runtime中的例外:無法啓動組件

package com.thenewboston.thenewboston; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.RadioGroup; 
import android.widget.RadioGroup.OnCheckedChangeListener; 
import android.widget.TextView; 

public class OpenedClass extends Activity implements View.OnClickListener, 
    OnCheckedChangeListener { 

TextView question, test; 
Button returnData; 
RadioGroup selectionList; 
String gotBread; 
String setData; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.send); 
    initialize(); 

private void initialize() { 
    // TODO Auto-generated method stub 
    question = (TextView) findViewById(R.id.tvQuestion); 
    test = (TextView) findViewById(R.id.tvTest); 
    returnData = (Button) findViewById(R.id.bResults); 
    selectionList = (RadioGroup) findViewById(R.id.rgAnswers); 
    selectionList.setOnCheckedChangeListener(this); 
    returnData.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    Intent person = new Intent(); 
    Bundle backpack = new Bundle(); 
    backpack.putString("answer", setData); 
    person.putExtras(backpack); 
    setResult(RESULT_OK,person); 
    finish(); 
} 

@Override 
public void onCheckedChanged(RadioGroup group, int checkedId) { 
    // TODO Auto-generated method stub 
    switch (checkedId) { 
    case R.id.rCrazy: 
     setData = "Probably Right !"; 
     break; 
    case R.id.rSexy: 
     setData = "Definitely Right !"; 
     break; 
    case R.id.rBoth: 
     setData = "Spot On !"; 
     break; 
    } 
    test.setText(setData); 
} 

} 




這是logcat的輸出:

01-05 15:59:54.605: E/AndroidRuntime(313): FATAL EXCEPTION: main 
01-05 15:59:54.605: E/AndroidRuntime(313): java.lang.RuntimeException: Unable to start  activity ComponentInfo{com.thenewboston.thenewboston/com.thenewboston.thenewboston.OpenedClass}: java.lang.NullPointerException 
01-05 15:59:54.605: E/AndroidRuntime(313): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
01-05 15:59:54.605: E/AndroidRuntime(313): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
01-05 15:59:54.605: E/AndroidRuntime(313): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
01-05 15:59:54.605: E/AndroidRuntime(313): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
01-05 15:59:54.605: E/AndroidRuntime(313): at android.os.Handler.dispatchMessage(Handler.java:99) 
01-05 15:59:54.605: E/AndroidRuntime(313): at android.os.Looper.loop(Looper.java:123) 
01-05 15:59:54.605: E/AndroidRuntime(313): at android.app.ActivityThread.main(ActivityThread.java:3683) 
01-05 15:59:54.605: E/AndroidRuntime(313): at java.lang.reflect.Method.invokeNative(Native Method) 
01-05 15:59:54.605: E/AndroidRuntime(313): at java.lang.reflect.Method.invoke(Method.java:507) 
01-05 15:59:54.605: E/AndroidRuntime(313): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
01-05 15:59:54.605: E/AndroidRuntime(313): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
01-05 15:59:54.605: E/AndroidRuntime(313): at dalvik.system.NativeStart.main(Native Method) 
01-05 15:59:54.605: E/AndroidRuntime(313): Caused by: java.lang.NullPointerException 
01-05 15:59:54.605: E/AndroidRuntime(313): at com.thenewboston.thenewboston.OpenedClass.initialize(OpenedClass.java:44) 
01-05 15:59:54.605: E/AndroidRuntime(313): at com.thenewboston.thenewboston.OpenedClass.onCreate(OpenedClass.java:25) 
01-05 15:59:54.605: E/AndroidRuntime(313): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
01-05 15:59:54.605: E/AndroidRuntime(313): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
01-05 15:59:54.605: E/AndroidRuntime(313): ... 11 more 


因爲你們可以看到,它告訴我,錯誤的是在我的代碼44行和44行是

 returnData.setOnClickListener(this); 

當我這樣做不是沒有我不理解的事,當我註釋此行,它會顯示我的佈局。任何想法爲什麼發生這種情況?

+3

'returnData'爲空。檢查ID在你的佈局XML – Raghunandan

+0

感謝Raghunandan您的評論幫助了很多 –

回答

1

從您的評論下面marcin_j發佈

<Button android:id="@+id/bReturn" // id is bReturn 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Return" > 
</Button> 

更改此

returnData = (Button) findViewById(R.id.bResults); 

returnData = (Button) findViewById(R.id.bReturn); 
+0

謝謝,我只是做了它,而你正在發佈 –

+0

@jazzb我無法發佈它之前,我din't知道你在XML有什麼。但是,ID是錯誤的,因此NPE – Raghunandan

+0

先生我是從我的文章發表的上述評論,我是從我的心臟的深度真的心存感激。這是我的壞,我沒有張貼的XML文件,並再次非常感謝主席先生。 –

相關問題