简体   繁体   English

java.lang.runtime异常:无法启动组件

[英]java.lang.runtime Exception : unable to start component

I am learning java through a video tutorial but I am having a strange problem I create a class named OpenedClass in java and implements two classes. 我正在通过一个视频教程学习Java,但是遇到一个奇怪的问题,我在Java中创建了一个名为OpenedClass的类并实现了两个类。
One is for OnClickListener and 2nd is OnCheckChangeListener but when i run the application on emulator it always give me the error for the calling OnClickListener . 第一个是OnClickListener ,第二个是OnCheckChangeListener但是当我在模拟器上运行应用程序时,它总是给我调用OnClickListener的错误。
The code for my java class is: 我的Java类的代码是:

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);
}

}




and this is the output for logcat: 这是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


as you guys can see that it tells me that the error is on line 44 and the line 44 in my code is 你们可以看到它告诉我错误在第44行,而我代码中的第44行是

        returnData.setOnClickListener(this);

I did not understand it at all when I comment this line it will show me the layout when I don't it didn't. 当我对此行发表评论时,我一点也不理解,如果不这样做,它将显示布局。 Any idea why is this happening ? 知道为什么会这样吗?

From your comments below marcin_j post 从下面的评论marcin_j发表

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

Change this 改变这个

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM