Java OnClickListener未按预期运行
我有一个在线监听器:Java OnClickListener未按预期运行,java,android,onclicklistener,buttonclick,Java,Android,Onclicklistener,Buttonclick,我有一个在线监听器: mAssistUpdateButton = (Button) findViewById(R.id.assist_instr_btn); mAssistUpdateButton.setOnClickListener(this); 这应该叫: public void onClick(View v) { if (v == mAssistUpdateButton) { 但代码中似乎没有达到这一点。我已经看了好几遍,似乎都不知道我做错了什么 非常感谢您的任
mAssistUpdateButton = (Button) findViewById(R.id.assist_instr_btn);
mAssistUpdateButton.setOnClickListener(this);
这应该叫:
public void onClick(View v) {
if (v == mAssistUpdateButton) {
但代码中似乎没有达到这一点。我已经看了好几遍,似乎都不知道我做错了什么
非常感谢您的任何意见
资料来源:
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ConfigFinalActivity extends Activity implements OnClickListener {
private static final String TAG = "ConfigActivity";
TelephonyManager tm;
AlertDialog mErrorAlert = null;
private Notification mNotification = null;
private Button mXButton = null;
private Button mAssistUpdateButton = null;
private Button mAssistInstrButton = null;
private Button 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 = 8;
int i, g = 0, result = 0;
String Result = "";
public static ContentValues Values = new ContentValues();
XmlParserHandlerFinal handler;
public static final Uri APN_TABLE_URI = Uri
.parse("content://telephony/carriers");
public static String Base_URL = "https://www.mysettings.com/";
public static InputStream stream = null;
UpdateActivity update;
public static String status;
/** 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();
nameArr = update.getnameArr();
ApnArr = update.getApnArr();
mmscArr = update.getMMSCArr();
mmsproxyArr = update.getMmscProxyArr();
mmsportArr = update.getMmsPortArr();
proxyArr = update.getMmscProxyArr();
portArr = update.getMmsPortArr();
count = update.getCount();
if (ApnArr.isEmpty() || mmscArr.isEmpty() || mmsportArr.isEmpty()
|| mmsproxyArr.isEmpty() || proxyArr.isEmpty()
|| portArr.isEmpty()) {
tryagain();
} else if (version < VERSION_CODES.ICE_CREAM_SANDWICH) {
// 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();
// showAlert(getString(R.string.unchanged_net10_dialog));
}
} else {// ICS and later versions
// Reduce number of steps to 6
TotalSteps = 6;
setContentView(R.layout.assist_instructions);
// String assistUpdate = getString(R.string.instructions_1);
// CharSequence styledText = Html.fromHtml(assistUpdate);
TextView assistText = (TextView) findViewById(R.id.apn_app_text_cta2);
// assistText.setText(styledText);
mAssistUpdateButton = (Button) findViewById(R.id.assist_instr_btn);
mAssistUpdateButton.setOnClickListener(this);
}
}
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.instructions_3),
// 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:
// First set of instructions for step by step process to update
// the APN Settings to the corresponding layout
//setContentView(R.layout.screen5);
//// String one_text = String.format(getString(R.string.one),
//// TotalSteps);
//// CharSequence styledText_one = Html.fromHtml(one_text);
// TextView one = (TextView) findViewById(R.id.text_one);
// one.setTextSize(18);
// one.setText(styledText_one);
// String two_text = String.format(getString(R.string.two),
// TotalSteps);
// CharSequence styledText_two = Html.fromHtml(two_text);
// TextView two = (TextView) findViewById(R.id.text_two);
// two.setTextSize(18);
// two.setText(styledText_two);
// String three_text = String.format(getString(R.string.three),
// TotalSteps);
// CharSequence styledText_three = Html.fromHtml(three_text);
// TextView three = (TextView) findViewById(R.id.text_three);
// three.setTextSize(18);
// three.setText(styledText_three);
mAssistUpdateButton = (Button) findViewById(R.id.assist_instr_btn);
mAssistUpdateButton.setOnClickListener(this);
mInstructionNumber++;
break;
case 2:
// second set of instructions for step by step process to update
// the APN Settings to the corresponding layout
// setContentView(R.layout.screen6);
// String four_text = String.format(getString(R.string.four),
// TotalSteps);
// styledText = Html.fromHtml(four_text);
// TextView four = (TextView) findViewById(R.id.text_four);
// four.setTextSize(18);
// four.setText(styledText);
// String five_text = String.format(getString(R.string.five),
// TotalSteps);
// styledText = Html.fromHtml(five_text);
// TextView five = (TextView) findViewById(R.id.text_five);
// five.setTextSize(18);
// five.setText(styledText);
// String six_text = String.format(getString(R.string.six),
// TotalSteps);
// styledText = Html.fromHtml(six_text);
// TextView six = (TextView) findViewById(R.id.text_six);
// six.setTextSize(18);
// six.setText(styledText);
// // String seven_text = String.format(getString(R.string.seven),
// TotalSteps);
// styledText = Html.fromHtml(seven_text);
// TextView seven = (TextView) findViewById(R.id.text_seven);
// seven.setTextSize(18);
// seven.setText(styledText);
// String eight_text = String.format(getString(R.string.eight),
// TotalSteps);
// styledText = Html.fromHtml(eight_text);
// TextView eight = (TextView) findViewById(R.id.text_eight);
// eight.setTextSize(18);
// eight.setText(styledText);
// just for testing
Result = "success";
mAssistUpdateButton = (Button) findViewById(R.id.assist_instr_btn);
if (Result.equalsIgnoreCase("success")) {
mAssistUpdateButton.setOnClickListener(this);
} else if (Result.equalsIgnoreCase("failure")) {
// // Displaying final layout after failure of Post-ICS
// settings
// read the result in the response ..if success go ahead
// else load the tryagain screen
// setContentView(R.layout.tryagain);
// 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);
}
mInstructionNumber++;
break;
case 3:
// final set of instructions
// setContentView(R.layout.assist_instructions_update);
// String last_instr_text = String.format(
// getString(R.string.instructions_5), TotalSteps);
// styledText = Html.fromHtml(last_instr_text);
// TextView last_text = (TextView) findViewById(R.id.apn_app_text_cta3);
// last_text.setText(styledText);
// // String last_instr_text1 = String.format(
// getString(R.string.thatsit), TotalSteps);
// styledText = Html.fromHtml(last_instr_text1);
// TextView last_text1 = (TextView) findViewById(R.id.thatsIt);
// last_text1.setText(styledText);
String mreadAgaintext = String
.format(getString(R.string.read_again_text));
styledText = Html.fromHtml(mreadAgaintext);
mAssistInstrButton = (Button) findViewById(R.id.assist_instr_btn);
//ReadAgainButton = (Button) findViewById(R.id.read_again_btn);
//ReadAgainButton.setText(styledText);
mAssistInstrButton.setOnClickListener(this);
//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();
import java.io.IOException;
导入java.io.InputStream;
导入java.util.ArrayList;
导入javax.xml.parsers.parserConfiguration异常;
导入org.xml.sax.SAXException;
导入android.annotation.SuppressLint;
导入android.annotation.TargetApi;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.app.Notification;
导入android.app.NotificationManager;
导入android.app.pendingent;
导入android.content.ContentResolver;
导入android.content.ContentValues;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.database.Cursor;
导入android.database.SQLException;
导入android.net.Uri;
导入android.os.Build;
导入android.os.Build.VERSION\u代码;
导入android.os.Bundle;
导入android.provider.Settings;
导入android.telephony.TelephonyManager;
导入android.text.Html;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.TextView;
公共类ConfigFinalActivity扩展活动实现OnClickListener{
私有静态最终字符串TAG=“ConfigActivity”;
TelephonyManager tm;
AlertDialog mErrorAlert=null;
私有通知mNotification=null;
私有按钮mXButton=null;
private Button-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();
公共静态整数计数;
公共静态int TotalSteps=8;
int i,g=0,结果=0;
字符串结果=”;
公共静态ContentValues=新ContentValues();
XmlParserHandlerFinal处理程序;
公共静态最终Uri APN_表_Uri=Uri
.parse(“content://telephony/carriers");
公共静态字符串Base_URL=”https://www.mysettings.com/";
公共静态InputStream=null;
更新活动更新;
公共静态字符串状态;
/**在首次创建活动时调用*/
@SuppressLint(“新API”)
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
int version=android.os.Build.version.SDK\u int;
tm=(电话管理器)getSystemService(电话服务);
update=新的UpdateActivity();
nameArr=update.getnameArr();
ApnArr=update.getApnArr();
mmscArr=update.getMMSCArr();
mmsproxyArr=update.getMmscProxyArr();
mmsportArr=update.getMmsPortArr();
proxyArr=update.getMmscProxyArr();
portArr=update.getMmsPortArr();
count=update.getCount();
if(ApnArr.isEmpty()| | mmscar.isEmpty()| | mmsportar.isEmpty())
||mmsproxyArr.isEmpty()| | proxyArr.isEmpty()
||portArr.isEmpty()){
tryagain();
}否则如果(版本<版本代码.冰淇淋\u三明治){
//更新APN表
试一试{
结果=updateTable();
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}//使用此原子调用更新设置
捕获(SAXE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(ParserConfiguration异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
如果(结果!=-1){
status=“success”;
}否则{
status=“失败”;
}
如果(状态等于(“成功”)){
完全更新();
}else if(状态等于(“失败”)){
tryagain();
//showAlert(getString(R.string.unchanged_net10_对话框));
}
}else{//ICS和更高版本
//将步骤数减少到6个
总步数=6;
setContentView(右布局、辅助指示);
//String assistUpdate=getString(R.String.instructions_1);
//CharSequence styledText=Html.fromHtml(assistUpdate);
TextView assistText=(TextView)findViewById(R.id.apn\u app\u text\u cta2);
//assistText.setText(styledText);
mAssistUpdateButton=(按钮)findViewById(R.id.assist\u instr\u btn);
mAssistUpdateButton.setOnClickListener(此);
}
}
公共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.instructions_3),
//总步骤);
//styledText=Html.from
public void onClick(View v) {
switch(v.getId()) {
case R.id.assist_instr_btn:
case R.id.button2:
}
}
import android.view.View.OnclickListener
public void onClick(View v) {
switch(v.getId()) {
case R.id.btn_1:
...
break;
case R.id.btn_2:
...
break;
}
}
public void onClick(View v) {
if(v.equals(mAssistsUpdateButton )) {
//your code
}else if (v.equals(mButton2 )) {
//your code
}
}