我一直在尝试为学校项目测试Android应用程序开发。我正在测试2个单选按钮,它们将改变文本字段。我想我的代码是正确的,但我得到了一个"停止工作"每当我试图运行它。我使用的是API 11+
这是我的XML文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ServicesActivity" >
<RadioGroup
android:id="@+id/radioGroupDepart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<RadioButton
android:id="@+id/radioBtnMalta"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textTest"
android:layout_marginTop="26dp"
android:text="Malta" />
<RadioButton
android:id="@+id/radioBtnGozo"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/radioBtnMalta"
android:layout_alignBottom="@+id/radioBtnMalta"
android:layout_alignParentRight="true"
android:layout_marginRight="40dp"
android:text="Gozo" />
</RadioGroup>
<TextView
android:id="@+id/textTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
这是我的.java文件:
package com.example.services;
跳过进口,因为他们可以正确显示
public class ServicesActivity extends Activity implements RadioGroup.OnCheckedChangeListener {
//Radio Group Declaration and Edit Text
EditText et1;
RadioButton maltaRB;
RadioButton gozoRB;
RadioGroup gozoMaltaRBGroup;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_services);
//Radio Group
et1=(EditText)findViewById(R.id.textTest);
maltaRB=(RadioButton)findViewById(R.id.MaltaRadioBtn);
gozoRB=(RadioButton)findViewById(R.id.GozoRadioBtn);
gozoMaltaRBGroup=(RadioGroup)findViewById(R.id.radioGroupDepart);
gozoMaltaRBGroup.setOnCheckedChangeListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.services, menu);
return true;
}
TextView textTest = (TextView)findViewById(R.id.textTest);
@Override
public void onCheckedChanged(RadioGroup gozoMaltaRBGroup, int i) {
if(i==maltaRB.getId()){
textTest.setText("Leaving from Malta");
}
else if(i==gozoRB.getId()){
textTest.setText("Leaving from Gozo");
}
}
}
最后这是来自LogCat的错误日志:
10-21 15:28:03.802: D/AndroidRuntime(829): Shutting down VM
10-21 15:28:03.802: W/dalvikvm(829): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
10-21 15:28:03.821: E/AndroidRuntime(829): FATAL EXCEPTION: main
10-21 15:28:03.821: E/AndroidRuntime(829): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.services/com.example.services.ServicesActivity}: java.lang.NullPointerException
10-21 15:28:03.821: E/AndroidRuntime(829): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
10-21 15:28:03.821: E/AndroidRuntime(829): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-21 15:28:03.821: E/AndroidRuntime(829): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-21 15:28:03.821: E/AndroidRuntime(829): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-21 15:28:03.821: E/AndroidRuntime(829): at android.os.Handler.dispatchMessage(Handler.java:99)
10-21 15:28:03.821: E/AndroidRuntime(829): at android.os.Looper.loop(Looper.java:137)
10-21 15:28:03.821: E/AndroidRuntime(829): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-21 15:28:03.821: E/AndroidRuntime(829): at java.lang.reflect.Method.invokeNative(Native Method)
10-21 15:28:03.821: E/AndroidRuntime(829): at java.lang.reflect.Method.invoke(Method.java:525)
10-21 15:28:03.821: E/AndroidRuntime(829): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-21 15:28:03.821: E/AndroidRuntime(829): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-21 15:28:03.821: E/AndroidRuntime(829): at dalvik.system.NativeStart.main(Native Method)
10-21 15:28:03.821: E/AndroidRuntime(829): Caused by: java.lang.NullPointerException
10-21 15:28:03.821: E/AndroidRuntime(829): at android.app.Activity.findViewById(Activity.java:1853)
10-21 15:28:03.821: E/AndroidRuntime(829): at com.example.services.ServicesActivity.<init>(ServicesActivity.java:42)
10-21 15:28:03.821: E/AndroidRuntime(829): at java.lang.Class.newInstanceImpl(Native Method)
10-21 15:28:03.821: E/AndroidRuntime(829): at java.lang.Class.newInstance(Class.java:1130)
10-21 15:28:03.821: E/AndroidRuntime(829): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
10-21 15:28:03.821: E/AndroidRuntime(829): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
10-21 15:28:03.821: E/AndroidRuntime(829): ... 11 more
任何帮助将不胜感激!
答案 0 :(得分:1)
这些行正在返回null
maltaRB=(RadioButton)findViewById(R.id.MaltaRadioBtn);
gozoRB=(RadioButton)findViewById(R.id.GozoRadioBtn);
检查id
。 Java中的id
与layout
文件中的maltaRB=(RadioButton)findViewById(R.id.radioBtnMalta);
gozoRB=(RadioButton)findViewById(R.id.radioBtnGozo);
不同。
将其更改为
id
这应该有效。或者,您可以更改xml中的NPE
,但无论是哪种方式都必须匹配,或者在尝试调用函数或在其上设置侦听器时获得null
。
此外,这将返回TextView textTest = (TextView)findViewById(R.id.textTest);
null
您需要在函数内部对其进行初始化,否则它将返回setContentView()
,因为它将在View
之前运行。只需声明并初始化它就像你是其他{{1}}一样。
答案 1 :(得分:1)
你有额外的代码行:
TextView textTest = (TextView)findViewById(R.id.textTest);
你的方法onCreateOptionsMenu()和onCheckedChanged()之间的。这条线不适合那里,它必须放在一些方法里面。可能你可以将它除以两行:声明
TextView textTest;
和初始化
textTest = (TextView)findViewById(R.id.textTest);
但这取决于你真正想要的东西。