我在线上遇到强制关闭错误:
mReadAgainButton.setOnClickListener(this);
我不确定为什么。我只是想为 mReadAgainButton 设置一个 onclick 侦听器,但是应用程序崩溃并出现致命异常,并且我的 logcat 中的信息很少,无法提示可能导致它的原因。
日志猫:
09-26 15:44:51.540: E/AndroidRuntime(7918): FATAL EXCEPTION: main
09-26 15:44:51.540: E/AndroidRuntime(7918): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.example.settings/com.project.example.settings.ConfigFinalActivity}: java.lang.NullPointerException
09-26 15:44:51.540: E/AndroidRuntime(7918): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2062)
09-26 15:44:51.540: E/AndroidRuntime(7918): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2087)
09-26 15:44:51.540: E/AndroidRuntime(7918): at android.app.ActivityThread.access$600(ActivityThread.java:133)
09-26 15:44:51.540: E/AndroidRuntime(7918): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1198)
09-26 15:44:51.540: E/AndroidRuntime(7918): at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 15:44:51.540: E/AndroidRuntime(7918): at android.os.Looper.loop(Looper.java:137)
09-26 15:44:51.540: E/AndroidRuntime(7918): at android.app.ActivityThread.main(ActivityThread.java:4777)
09-26 15:44:51.540: E/AndroidRuntime(7918): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 15:44:51.540: E/AndroidRuntime(7918): at java.lang.reflect.Method.invoke(Method.java:511)
09-26 15:44:51.540: E/AndroidRuntime(7918): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
09-26 15:44:51.540: E/AndroidRuntime(7918): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
09-26 15:44:51.540: E/AndroidRuntime(7918): at dalvik.system.NativeStart.main(Native Method)
09-26 15:44:51.540: E/AndroidRuntime(7918): Caused by: java.lang.NullPointerException
09-26 15:44:51.540: E/AndroidRuntime(7918): at com.project.example.settings.ConfigFinalActivity.onCreate(ConfigFinalActivity.java:141)
09-26 15:44:51.540: E/AndroidRuntime(7918): at android.app.Activity.performCreate(Activity.java:5008)
09-26 15:44:51.540: E/AndroidRuntime(7918): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-26 15:44:51.540: E/AndroidRuntime(7918): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2026)
09-26 15:44:51.540: E/AndroidRuntime(7918): ... 11 more
资源:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int version = android.os.Build.VERSION.SDK_INT;
tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
update = new UpdateActivity();
getArrayLists();
if (ApnArr.isEmpty() || mmscArr.isEmpty()) {
tryagain();
} else if (version < VERSION_CODES.ICE_CREAM_SANDWICH) {
SettingsDbAdapter.setArrayLists(nameArr, ApnArr, mmscArr,
mmsproxyArr, mmsportArr, proxyArr, portArr, count,
ConfigFinalActivity.this);
// Update APN table
try {
result = updateTable();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}// Settings updated with this atomic call
catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (result != -1) {
status = "success";
} else {
status = "failure";
}
if (status.equals("success")) {
completeUpdate();
} else if (status.equals("failure")) {
tryagain();
}
} else {// ICS and later versions
// Reduce number of steps to 10
TotalSteps = 10;
setContentView(R.layout.assist_update);
String assistUpdate = getString(R.string.instructions_1);
mAssistUpdateButton = (ImageView) findViewById(R.id.assist_update_btn);
mAssistUpdateButton.setOnClickListener(this);
mReadAgainButton = (TextView) findViewById(R.id.read_again_btn);
mReadAgainButton.setOnClickListener(this);
}
}
private void getArrayLists() {
nameArr = update.getnameArr();
ApnArr = update.getApnArr();
mmscArr = update.getMMSCArr();
mmsproxyArr = update.getMmscProxyArr();
mmsportArr = update.getMmsPortArr();
proxyArr = update.getProxyArr();
portArr = update.getPortArr();
count = update.getCount();
queryResult = update.getResult();
}
public void onClick(View v) {
if (v == mAssistUpdateButton) {
// Update button for ICS and up is selected
// Get the TextView in the Assist Update UI
TextView tv = (TextView) findViewById(R.id.apn_app_text_cta2);
String text = "";
CharSequence styledText = text;
switch (mInstructionNumber) {
case 0:
// Retrieve the instruction string resource corresponding the
// 2nd set of instructions
text = String.format(getString(R.string.apn_app_text_instr),
TotalSteps);
styledText = Html.fromHtml(text);
// Update the TextView with the correct set of instructions
tv.setText(styledText);
// Increment instruction number so the correct instructions
// string resource can be retrieve the next time the update
// button is pressed
mInstructionNumber++;
break;
case 1:
text = getString(R.string.apn_app_text_instr2);
styledText = Html.fromHtml(text);
tv.setText(styledText);
// Increment instruction number so the correct instructions
// string resource can be retrieve the next time the update
// button is pressed
mInstructionNumber++;
break;
case 2:
// Final set of instructions-Change to the corresponding layout
setContentView(R.layout.assist_instructions);
String assistUpdateInstr = String.format(
getString(R.string.apn_app_text_instr3), TotalSteps);
styledText = Html.fromHtml(assistUpdateInstr);
TextView assistInstrText = (TextView) findViewById(R.id.updated_text);
assistInstrText.setText(styledText);
mAssistInstrButton = (Button) findViewById(R.id.assist_instr_btn);
mAssistInstrButton.setOnClickListener(this);
}
} else if (v == mAssistInstrButton) {
// "LET'S DO THIS" Button in final instructions screen for ICS and
// up is selected
Values = getValues();
startActivity(new Intent(Settings.ACTION_APN_SETTINGS));
try {
showNotification();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finish();
} else if (v == mAssistInstrButton) {
startActivity(new Intent(Settings.ACTION_APN_SETTINGS));
try {
showNotification();
} catch (SAXException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
showNotification();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finish();
} else if (v == mReadAgainButton) {
// go back to set of instructions if read again is selected
TextView tv = (TextView) findViewById(R.id.apn_app_text_cta2);
String text = "";
CharSequence styledText = text;
switch (mInstructionNumber) {
case 0:
// Retrieve the instruction string resource corresponding the
// 2nd set of instructions
text = String.format(getString(R.string.apn_app_text_instr),
TotalSteps);
styledText = Html.fromHtml(text);
// Update the TextView with the correct set of instructions
tv.setText(styledText);
// Increment instruction number so the correct instructions
// string resource can be retrieve the next time the update
// button is pressed
mInstructionNumber++;
break;
case 1:
text = getString(R.string.apn_app_text_instr2);
styledText = Html.fromHtml(text);
tv.setText(styledText);
// Increment instruction number so the correct instructions
// string resource can be retrieve the next time the update
// button is pressed
mInstructionNumber++;
break;
case 2:
// Final set of instructions-Change to the corresponding layout
setContentView(R.layout.assist_instructions);
String assistUpdateInstr = String.format(
getString(R.string.apn_app_text_instr3), TotalSteps);
styledText = Html.fromHtml(assistUpdateInstr);
TextView assistInstrText = (TextView) findViewById(R.id.updated_text);
assistInstrText.setText(styledText);
mAssistInstrButton = (Button) findViewById(R.id.assist_instr_btn);
mAssistInstrButton.setOnClickListener(this);
}
}
}