OnClick侦听器未按预期运行

我在TextViewmReadAgainButton )上设置了mReadAgainButton ,它应该从头开始重新加载一组指令(最初通过名为mAssistUpdateButtonButton上的mAssistUpdateButton )但是当点击TextView mReadAgainButton似乎没有任何事情发生。

 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); mReadAgainButton = (TextView) findViewById(R.id.read_again_btn); mReadAgainButton.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); mReadAgainButton = (TextView) findViewById(R.id.read_again_btn); mReadAgainButton.setOnClickListener(this); } } } 

完整来源:

 public class ConfigFinalActivity extends Activity implements OnClickListener { private static final String TAG = "ConfigFinalActivity"; TelephonyManager tm; AlertDialog mErrorAlert = null; private Notification mNotification = null; private Button mXButton = null; private ImageView mAssistUpdateButton = null; private Button mAssistInstrButton = null; private TextView mReadAgainButton = null; private int mInstructionNumber = 0; public static ArrayList NameArr = new ArrayList(); public static ArrayList ValueArr = new ArrayList(); public static ArrayList nameArr = new ArrayList(); public static ArrayList ApnArr = new ArrayList(); public static ArrayList mmscArr = new ArrayList(); public static ArrayList mmsportArr = new ArrayList(); public static ArrayList mmsproxyArr = new ArrayList(); public static ArrayList portArr = new ArrayList(); public static ArrayList proxyArr = new ArrayList(); public static int count; public static int TotalSteps = 10; int i, g = 0, result = 0; public static ContentValues Values = new ContentValues(); public static final Uri APN_TABLE_URI = Uri .parse("content://telephony/carriers"); public static InputStream stream = null; UpdateActivity update; public static String status, queryResult = ""; /** Called when the activity is first created. */ @SuppressLint("NewApi") @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  0) { values.put("name", nameArr.get(i)); } if (ApnArr.size() > 0) { values.put("apn", ApnArr.get(i)); } if (mmscArr.size() > 0) { values.put("mmsc", mmscArr.get(i)); } if (mmsproxyArr.size() > 0) { values.put("mmsproxy", mmsproxyArr.get(i)); } if (mmsportArr.size() > 0) { values.put("mmsport", mmsportArr.get(i)); } if (proxyArr.size() > 0) { values.put("proxy", proxyArr.get(i)); } if (portArr.size() > 0) { values.put("port", portArr.get(i)); } values.put("mcc", (getString(R.string.mcc))); if ((tm.getSimOperator()).equals(getString(R.string.numeric_tmo))) { values.put("numeric", getString(R.string.numeric_tmo)); values.put("mnc", (getString(R.string.mnc_tmo))); } else if ((tm.getSimOperator()) .equals(getString(R.string.numeric_att))) { values.put("numeric", getString(R.string.numeric_att)); values.put("mnc", (getString(R.string.mnc_att))); } values.put("type", getString(R.string.type)); return values; } // showAlert displays the text contained in message as an alert public void showAlert(String message) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(message).setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { ConfigFinalActivity.this.finish(); } }); mErrorAlert = builder.create(); mErrorAlert.show(); } // showErrorAlert displays an alert with layout and a title private void showErrorAlert(int layoutRes, String title) { AlertDialog.Builder builder = new AlertDialog.Builder(this); // Get the layout inflater LayoutInflater inflater = ConfigFinalActivity.this.getLayoutInflater(); // Inflate and set the layout for the dialog // Pass null as the parent view because its going in the dialog layout builder.setTitle(title) .setView(inflater.inflate(layoutRes, null)) .setPositiveButton(getString(R.string.assisted_button), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { 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(); } } }); mErrorAlert = builder.create(); mErrorAlert.show(); } // showNotification starts the process of sending notifications to the bar // to assist the user in updating the data settings on ICS and later // versions of Android @SuppressWarnings("deprecation") @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) void showNotification() throws SAXException, ParserConfigurationException { String field = getString(R.string.config_name_label); String value = Values.get("name").toString(); int mId = 1; String title = "1 of " + TotalSteps + " (Update " + field + ":)"; Notification.Builder mBuilder = new Notification.Builder(this) .setSmallIcon(R.drawable.notifications_icon) .setContentTitle(title).setContentText(value); Intent resultIntent = new Intent(this, NotificationActivityForMultiProf.class); resultIntent.putExtra(field, value); PendingIntent resultPendingIntent = PendingIntent.getActivity( getApplicationContext(), 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // mId allows you to update the notification later on. mNotification = mBuilder.getNotification(); mNotification.flags |= Notification.FLAG_AUTO_CANCEL; mNotificationManager.notify(mId, mNotification); finish(); } @Override protected void onSaveInstanceState(Bundle outState) { // TODO Auto-generated method stub super.onSaveInstanceState(outState); if (mNotification != null) { outState.putString("NOTIFICATIONB", mNotification.toString()); } } @Override protected void onRestart() { super.onRestart(); if (mErrorAlert != null) mErrorAlert.dismiss(); } private int updateTable() throws IOException, SAXException, ParserConfigurationException { int insertResult = -1; // returned value if table is not properly updated try { @SuppressWarnings("unused") ContentValues values = new ContentValues(); // Query the carrier table for the current data settings Cursor c = getContentResolver().query(APN_TABLE_URI, null, "current=?", new String[] { "1" }, null); values = SettingsDbAdapter.copyRecordFields(c); // Copy the NET10 settings into values // Replace T-Mo/ATT Data settings if there is no SIM or // StraightTalk/T-Mo/ATT SIM is // present if (tm.getSimState() == TelephonyManager.SIM_STATE_ABSENT || (tm.getSimOperator()) .equals(getString(R.string.numeric_tmo))) { // delete all APNs before adding new APNs SettingsDbAdapter.DeleteAPNs("numeric=?", new String[] { getString(R.string.numeric_att) }, ConfigFinalActivity.this); // Insert StraightTalk Data Settings into Carrier table insertResult = SettingsDbAdapter.InsertAPN(count, ConfigFinalActivity.this); } else if (tm.getSimState() == TelephonyManager.SIM_STATE_ABSENT || (tm.getSimOperator()) .equals(getString(R.string.numeric_att))) { // Delete all APNs before adding new APNs SettingsDbAdapter.DeleteAPNs("numeric=?", new String[] { getString(R.string.numeric_att) }, ConfigFinalActivity.this); // Insert Straight Talk Data Settings into Carrier table insertResult = SettingsDbAdapter.InsertAPN(count, ConfigFinalActivity.this); } else // non Straight Talk/ non T-Mo SIM/non ATT SIM showAlert(getString(R.string.insert_sm_dialog)); } catch (SecurityException e) { showErrorAlert(R.layout.assisted_setting, getString(R.string.assited_title)); Log.d(TAG, e.getMessage()); } return insertResult; } private void completeUpdate() { // Displaying final layout after pre-ICS automatic settings update setContentView(R.layout.completion); TextView mCompleted = (TextView) findViewById(R.id.done_text); String mDoneText = String.format(getString(R.string.done_text)); CharSequence styledText = Html.fromHtml(mDoneText); mCompleted.setText(styledText); mXButton = (Button) findViewById(R.id.x_button); mXButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); } public void tryagain() { // Displaying final layout after failure of pre-ICS automatic settings // update setContentView(R.layout.tryagain); String tryAgainText = ""; CharSequence styledTryAgainText; tryAgainText = String.format(getString(R.string.tryagain_text1), TotalSteps); styledTryAgainText = Html.fromHtml(tryAgainText); TextView tryAgain1 = (TextView) findViewById(R.id.tryagain_text1); tryAgain1.setText(styledTryAgainText); tryAgainText = String.format(getString(R.string.tryagain_text2), TotalSteps); styledTryAgainText = Html.fromHtml(tryAgainText); TextView tryAgain2 = (TextView) findViewById(R.id.tryagain_text2); tryAgain2.setText(styledTryAgainText); tryAgainText = String.format(getString(R.string.tryagain_text3), TotalSteps); styledTryAgainText = Html.fromHtml(tryAgainText); TextView tryAgain3 = (TextView) findViewById(R.id.tryagain_text3); tryAgain3.setText(styledTryAgainText); } // This function return a cursor to the table holding the // the APN configurations (Carrier table) public Cursor getConfigTableCursor() { return getContentResolver() .query(APN_TABLE_URI, null, null, null, null); } } 

将对象与==运算符进行比较是一个坏主意。 尝试这样的事情:

 public void onClick(View v) { switch(v.getId()) { case R.id.yourButton: switch(mInstructionNumber) { case 0: case 1: ... } case R.id.ofmAssistInstrButton: case R.id.ofmAssistInstrButton: //I think you made a typo here since they are the same name default: //your else statement stuff ... } }