我正在实施一个设置页面,其中包含一个广播组和一个用于保存单选按钮选项的按钮。但是,当我单击“保存”按钮时,应用程序将自动崩溃。请帮忙。
以下是我的代码
Settings.java
package com.example.sunny.mynote;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.EditText;
/**
* Created by Sunny on 19/04/2015.
*/
public class Settings extends Activity {
private RadioGroup RadioGroup1;
private RadioButton rdbRed, rdbBlue, rdbOrange;
private Button btnSave;
private EditText textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
View view = LayoutInflater.from(getApplication()).inflate(R.layout.settings, null);
RadioGroup1 = (RadioGroup) findViewById(R.id.RadioGroup1);
RadioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.rdbRed)
{
Toast.makeText(getApplicationContext(), "choice: Red",
Toast.LENGTH_SHORT).show();
RadioGroup1.check(R.id.rdbRed);
}
else if(checkedId == R.id.rdbBlue)
{
Toast.makeText(getApplicationContext(), "choice: Blue",
Toast.LENGTH_SHORT).show();
RadioGroup1.check(R.id.rdbBlue);
}
else
{
Toast.makeText(getApplicationContext(), "choice: Orange",
Toast.LENGTH_SHORT).show();
RadioGroup1.check(R.id.rdbOrange);
}
}
});
Intent intent = new Intent(this, NoteEditorActivity.class);
rdbRed = (RadioButton) findViewById(R.id.rdbRed);
rdbBlue = (RadioButton) findViewById(R.id.rdbBlue);
rdbOrange = (RadioButton) findViewById(R.id.rdbOrange);
textView = (EditText) findViewById(R.id.noteText);
btnSave = (Button)findViewById(R.id.btn_Save);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedId = RadioGroup1.getCheckedRadioButtonId();
RadioGroup1.check(R.id.rdbRed);
if(selectedId == rdbRed.getId()) {
textView.setTextColor(Color.parseColor("#FF0000"));
} else if(selectedId == rdbBlue.getId()) {
textView.setTextColor(Color.parseColor("#0066FF"));
} else {
textView.setTextColor(Color.parseColor("#FF6600"));
}
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
}
logcat的
04-21 22:34:32.357 1606-1606/com.example.sunny.mynote D/AbsListView﹕ onVisibilityChanged() is called, visibility : 0
04-21 22:34:32.357 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:32.377 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:34.257 1606-1606/com.example.sunny.mynote D/AbsListView﹕ Get MotionRecognitionManager
04-21 22:34:34.277 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:34.327 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:34.327 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:34.357 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:34.367 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:34.907 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:34.907 1606-1606/com.example.sunny.mynote D/AbsListView﹕ onDetachedFromWindow
04-21 22:34:34.907 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:34.917 1606-1606/com.example.sunny.mynote W/ApplicationPackageManager﹕ getCSCPackageItemText()
04-21 22:34:34.927 1606-1606/com.example.sunny.mynote E/MoreInfoHPW_ViewGroup﹕ Parent view is not a TextView
04-21 22:34:35.237 1606-1606/com.example.sunny.mynote D/AbsListView﹕ onVisibilityChanged() is called, visibility : 4
04-21 22:34:35.237 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:36.897 1606-1606/com.example.sunny.mynote D/AbsListView﹕ onVisibilityChanged() is called, visibility : 0
04-21 22:34:36.897 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:36.907 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:40.757 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:40.787 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:40.787 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:40.797 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:40.817 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:40.867 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:41.357 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:41.357 1606-1606/com.example.sunny.mynote D/AbsListView﹕ onDetachedFromWindow
04-21 22:34:41.357 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:41.367 1606-1606/com.example.sunny.mynote W/ApplicationPackageManager﹕ getCSCPackageItemText()
04-21 22:34:41.367 1606-1606/com.example.sunny.mynote E/MoreInfoHPW_ViewGroup﹕ Parent view is not a TextView
04-21 22:34:41.637 1606-1606/com.example.sunny.mynote D/AbsListView﹕ onVisibilityChanged() is called, visibility : 4
04-21 22:34:41.637 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:46.297 1606-1606/com.example.sunny.mynote D/AbsListView﹕ onVisibilityChanged() is called, visibility : 0
04-21 22:34:46.297 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:46.317 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:46.337 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:48.257 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:48.257 1606-1606/com.example.sunny.mynote W/ApplicationPackageManager﹕ getCSCPackageItemText()
04-21 22:34:48.267 1606-1606/com.example.sunny.mynote E/MoreInfoHPW_ViewGroup﹕ Parent view is not a TextView
04-21 22:34:48.547 1606-1606/com.example.sunny.mynote D/AbsListView﹕ onVisibilityChanged() is called, visibility : 4
04-21 22:34:48.547 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:48.987 1606-1606/com.example.sunny.mynote D/AbsListView﹕ onVisibilityChanged() is called, visibility : 0
04-21 22:34:48.987 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:49.007 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:49.007 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:34:51.967 1606-1606/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:49:05.757 8215-8215/com.example.sunny.mynote D/AbsListView﹕ onVisibilityChanged() is called, visibility : 4
04-21 22:49:05.757 8215-8215/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:49:05.767 8215-8215/com.example.sunny.mynote D/AbsListView﹕ onVisibilityChanged() is called, visibility : 0
04-21 22:49:05.767 8215-8215/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:49:05.767 8215-8215/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:49:05.787 8215-8215/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:49:05.797 8215-8215/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:49:07.207 8215-8215/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:49:08.347 8215-8215/com.example.sunny.mynote D/AbsListView﹕ Get MotionRecognitionManager
04-21 22:49:08.357 8215-8215/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:49:08.407 8215-8215/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:49:08.407 8215-8215/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:49:08.457 8215-8215/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:49:08.467 8215-8215/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:49:08.967 8215-8215/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:49:08.967 8215-8215/com.example.sunny.mynote D/AbsListView﹕ onDetachedFromWindow
04-21 22:49:08.967 8215-8215/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:49:08.977 8215-8215/com.example.sunny.mynote W/ApplicationPackageManager﹕ getCSCPackageItemText()
04-21 22:49:08.977 8215-8215/com.example.sunny.mynote E/MoreInfoHPW_ViewGroup﹕ Parent view is not a TextView
04-21 22:49:09.287 8215-8215/com.example.sunny.mynote D/AbsListView﹕ onVisibilityChanged() is called, visibility : 4
04-21 22:49:09.287 8215-8215/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:49:10.747 8215-8215/com.example.sunny.mynote D/AndroidRuntime﹕ Shutting down VM
04-21 22:49:10.747 8215-8215/com.example.sunny.mynote W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x417e0da0)
04-21 22:49:10.757 8215-8215/com.example.sunny.mynote E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.sunny.mynote, PID: 8215
java.lang.NullPointerException
at com.example.sunny.mynote.Settings$2.onClick(Settings.java:73)
at android.view.View.performClick(View.java:4630)
at android.view.View$PerformClick.run(View.java:19339)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5335)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
04-21 22:49:12.687 8215-8215/com.example.sunny.mynote I/Process﹕ Sending signal. PID: 8215 SIG: 9
04-21 22:49:12.877 8307-8307/com.example.sunny.mynote W/ApplicationPackageManager﹕ getCSCPackageItemText()
04-21 22:49:12.877 8307-8307/com.example.sunny.mynote I/PersonaManager﹕ getPersonaService() name persona_policy
04-21 22:49:12.917 8307-8307/com.example.sunny.mynote E/MoreInfoHPW_ViewGroup﹕ Parent view is not a TextView
04-21 22:49:12.927 8307-8307/com.example.sunny.mynote D/AbsListView﹕ Get MotionRecognitionManager
04-21 22:49:12.957 8307-8307/com.example.sunny.mynote D/AbsListView﹕ onVisibilityChanged() is called, visibility : 4
04-21 22:49:12.957 8307-8307/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:49:12.967 8307-8307/com.example.sunny.mynote D/AbsListView﹕ onVisibilityChanged() is called, visibility : 0
04-21 22:49:12.967 8307-8307/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:49:12.977 8307-8307/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:49:12.997 8307-8307/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:49:13.017 8307-8307/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:49:13.027 8307-8307/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 22:49:43.997 8307-8307/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
04-21 23:01:30.597 8307-8307/com.example.sunny.mynote D/AbsListView﹕ onVisibilityChanged() is called, visibility : 4
04-21 23:01:30.597 8307-8307/com.example.sunny.mynote D/AbsListView﹕ unregisterIRListener() is called
答案 0 :(得分:3)
您的textView
是null
。确保您的内容视图布局layout/settings.xml
实际上包含EditText
,其ID为noteText
。
答案 1 :(得分:0)
单击收音机然后调用此行
时使用此选项 radioGroup.check(R.id.radioButtonman);
int valuew= radioGroup.getCheckedRadioButtonId();
valuew提供Id选中哪个单选按钮。
这条线丢失所以得到错误..