Java OnClick侦听器未按预期运行
我在Java OnClick侦听器未按预期运行,java,android,onclick,switch-statement,onclicklistener,Java,Android,Onclick,Switch Statement,Onclicklistener,我在TextView(mReadAgainButton)上设置了一个onClickListener,它应该从一开始就重新加载一组指令(最初通过onClickListener在名为mAssistUpdateButton的按钮上显示)但是,当单击TextViewmReadAgainButton时,似乎什么也没有发生 public void onClick(View v) { if (v == mAssistUpdateButton) { // Update
TextView
(mReadAgainButton
)上设置了一个onClickListener
,它应该从一开始就重新加载一组指令(最初通过onClickListener
在名为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<String> NameArr = new ArrayList<String>();
public static ArrayList<String> ValueArr = new ArrayList<String>();
public static ArrayList<String> nameArr = new ArrayList<String>();
public static ArrayList<String> ApnArr = new ArrayList<String>();
public static ArrayList<String> mmscArr = new ArrayList<String>();
public static ArrayList<String> mmsportArr = new ArrayList<String>();
public static ArrayList<String> mmsproxyArr = new ArrayList<String>();
public static ArrayList<String> portArr = new ArrayList<String>();
public static ArrayList<String> proxyArr = new ArrayList<String>();
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 < 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);
}
}
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) {
switch (v.getId()) {
case R.id.assist_update_btn:
switch (mInstructionNumber) {
}
// 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);
}
//}
// switch (v.getId()) {
case R.id.assist_instr_btn:
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();
// / switch (v.getId()) {
case R.id.read_again_btn:
// switch(mInstructionNumber)
// go back to set of instructions if read again is selected
TextView tv2 = (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
tv2.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);
tv2.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 ContentValues getValues() {
ContentValues values = new ContentValues();
if (nameArr.size() > 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类ConfigFinalActivity扩展活动实现OnClickListener{
私有静态最终字符串标记=“ConfigFinalActivity”;
TelephonyManager tm;
AlertDialog mErrorAlert=null;
私有通知mNotification=null;
私有按钮mXButton=null;
private-ImageView-datebutton=null;
私有按钮mAssistInstrButton=null;
私有文本视图mReadAgainButton=null;
私有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();
公共静态整数计数;
公共静态整数总步长=10;
int i,g=0,结果=0;
公共静态ContentValues=新ContentValues();
公共静态最终Uri APN_表_Uri=Uri
.parse(“content://telephony/carriers");
公共静态InputStream=null;
更新活动更新;
公共静态字符串状态,queryResult=“”;
/**在首次创建活动时调用*/
@SuppressLint(“新API”)
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
int version=android.os.Build.version.SDK\u int;
tm=(电话管理器)getSystemService(电话服务);
update=新的UpdateActivity();
getArrayList();
if(ApnArr.isEmpty()| | mmscar.isEmpty()){
tryagain();
}否则如果(版本<版本代码.冰淇淋\u三明治){
SettingsDbAdapter.SETARRAYLIST(名称阵列、ApnArr、mmscArr、,
mmsproxyArr,mmsportArr,proxyArr,portArr,计数,
配置最终性。这一点);
//更新APN表
试一试{
结果=updateTable();
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}//使用此原子调用更新设置
捕获(SAXE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(ParserConfiguration异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
如果(结果!=-1){
status=“success”;
}否则{
status=“失败”;
}
如果(状态等于(“成功”)){
完全更新();
}else if(状态等于(“失败”)){
tryagain();
}
}else{//ICS和更高版本
//将步骤数减少到10
总步数=10;
setContentView(R.layout.assist_更新);
String assistUpdate=getString(R.String.instructions_1);
mAssistUpdateButton=(ImageView)findViewById(R.id.assist\u update\u btn);
mAssistUpdateButton.setOnClickListener(此);
}
}
私有void getArrayList(){
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();
}
公共void onClick(视图v){
开关(v.getId()){
案例R.id.assist\u update\u btn:
开关(最小结构编号){
}
//公共void onClick(视图v){
//如果(v==DateButton){
//已选择ICS和up的更新按钮
//在辅助更新UI中获取文本视图
TextView tv=(TextView)findViewById(R.id.apn\u app\u text\u cta2);
字符串文本=”;
CharSequence styledText=文本;
开关(最小结构编号){
案例0:
//检索对应的指令字符串资源
//第二套说明
text=String.format(getString(R.String.apn\u app\u text\u instr),
总步骤);
styledText=Html.fromHtml(文本);
//使用正确的指令集更新TextView
tv.setText(styledText);
//增加指令编号,以确保正确的指令
//可以在下次更新时检索字符串资源
//按钮按下了
mInstructionNumber++;
打破
案例1:
text=getString(R.string.apn\u app\u text\u instr2);
styledText=Html.fromHtml(文本);
tv.setText(styledText);
//增加指令编号,以确保正确的指令
//可以在下次更新时检索字符串资源
//按钮按下了
mInstructionNumber++;
打破
案例2:
//最终的指令集更改为相应的布局
setContentView(R.layout.assis
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
...
}
}