我正在通过视频教程学习java但我遇到一个奇怪的问题我在java中创建了一个名为OpenedClass
的类并实现了两个类。
一个用于OnClickListener
,第二个是OnCheckChangeListener
但是当我在模拟器上运行应用程序时,它总是给我调用OnClickListener
的错误。
我的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);
}
}
这是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);
当我评论这一行时,我根本不理解它,它会告诉我布局,当我不这样做时。知道为什么会这样吗?
答案 0 :(得分:1)
来自你的评论marcin_j post
<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);