0

我一直在尝试为一个学校项目测试 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

任何帮助将不胜感激!

4

2 回答 2

1

这些线路正在返回null

maltaRB=(RadioButton)findViewById(R.id.MaltaRadioBtn);
gozoRB=(RadioButton)findViewById(R.id.GozoRadioBtn);

检查ids。idJava 中的 s 与layout文件中的不同。

将它们更改为

maltaRB=(RadioButton)findViewById(R.id.radioBtnMalta);
gozoRB=(RadioButton)findViewById(R.id.radioBtnGozo);

这应该有效。或者,您可以更改idxml 中的 s ,但无论哪种方式它们都必须匹配,或者NPE在尝试调用函数或在它们上设置侦听器时得到 a 。

此外,这将返回null

TextView textTest = (TextView)findViewById(R.id.textTest);

您需要在函数内部对其进行初始化,否则它将返回null,因为它将在之前运行setContentView()。只需声明并初始化它,就像您是其他人一样View

于 2013-10-21T19:46:36.463 回答
1

你有额外的代码行:

TextView textTest = (TextView)findViewById(R.id.textTest);

在您的方法 onCreateOptionsMenu() 和 onCheckedChanged() 之间。这条线不适合那里,它必须放在某个方法中。可能您可以将其分为两行:声明

TextView textTest;

和初始化

textTest = (TextView)findViewById(R.id.textTest);

但这取决于你真正想要得到什么。

于 2013-10-21T19:53:28.713 回答