onClick的监听器无法正常工作 [英] OnClick Listener Is Not Function As Expected

查看:198
本文介绍了onClick的监听器无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 onClickListener 的TextView mReadAgainButton )这是应该重新装入一组指令从开始(通过 onClickListener 按钮最初显示名为 mAssistUpdateButton ),但是在的TextView mReadAgainButton 没有点击时似乎发生。

 公共无效的onClick(视图v){
        如果(V == mAssistUpdateButton){

            //被选为ICS及以上更新按钮
            //获取协助更新UI TextView的

            TextView的电视=(TextView中)findViewById(R.id.apn_app_text_cta2);
            字符串文本=;
            CharSequence中的StyledText =文本;
            开关(mInstructionNumber){

            情况下0:
                //获得对应的指令串资源
                //第二个指令集
                文=的String.Format(的getString(R.string.apn_app_text_instr)
                        TotalSteps);
                的StyledText = Html.fromHtml(文本);
                //更新与正确的指令集的TextView的
                tv.setText(的StyledText);
                所以正确的指令//增量指令号
                //串资源可以是检索下一时间的更新
                //按钮是pressed
                mInstructionNumber ++;
                打破;
            情况1:
                文=的getString(R.string.apn_app_text_instr2);
                的StyledText = Html.fromHtml(文本);
                tv.setText(的StyledText);
                所以正确的指令//增量指令号
                //串资源可以是检索下一时间的更新
                //按钮是pressed
                mInstructionNumber ++;
                打破;
            案例2:
                //决胜盘说明,更改为相应的布局

                的setContentView(R.layout.assist_instructions);
                字符串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 =(按钮)findViewById(R.id.assist_instr_btn);
                mAssistInstrButton.setOnClickListener(本);
                mReadAgainButton =(TextView中)findViewById(R.id.read_again_btn);
                mReadAgainButton.setOnClickListener(本);
            }
        }否则,如果(V == mAssistInstrButton){
            //LET'S做到这一点按钮,在最后指示屏幕ICS和
            //中选择了
            值= GetValues​​方法();
            startActivity(新意图(Settings.ACTION_APN_SETTINGS));
            尝试 {
                showNotification();
            }赶上(的SAXException E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }赶上(的ParserConfigurationException E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }
            完();
        }否则,如果(V == mAssistInstrButton){
            startActivity(新意图(Settings.ACTION_APN_SETTINGS));
            尝试 {
                showNotification();
            }赶上(的SAXException E1){
                // TODO自动生成的catch块
                e1.printStackTrace();
            }赶上(E1的ParserConfigurationException){
                // TODO自动生成的catch块
                e1.printStackTrace();
            }
            尝试 {
                showNotification();
            }赶上(的SAXException E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }赶上(的ParserConfigurationException E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }
            完();

        }否则,如果(V == mReadAgainButton){
            //返回指令集如果选择再读

            TextView的电视=(TextView中)findViewById(R.id.apn_app_text_cta2);
            字符串文本=;
            CharSequence中的StyledText =文本;
            开关(mInstructionNumber){

            情况下0:
                //获得对应的指令串资源
                //第二个指令集
                文=的String.Format(的getString(R.string.apn_app_text_instr)
                        TotalSteps);
                的StyledText = Html.fromHtml(文本);
                //更新与正确的指令集的TextView的
                tv.setText(的StyledText);
                所以正确的指令//增量指令号
                //串资源可以是检索下一时间的更新
                //按钮是pressed
                mInstructionNumber ++;
                打破;
            情况1:
                文=的getString(R.string.apn_app_text_instr2);
                的StyledText = Html.fromHtml(文本);
                tv.setText(的StyledText);
                所以正确的指令//增量指令号
                //串资源可以是检索下一时间的更新
                //按钮是pressed
                mInstructionNumber ++;
                打破;
            案例2:
                //决胜盘说明,更改为相应的布局

                的setContentView(R.layout.assist_instructions);
                字符串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 =(按钮)findViewById(R.id.assist_instr_btn);
                mAssistInstrButton.setOnClickListener(本);
                mReadAgainButton =(TextView中)findViewById(R.id.read_again_btn);
                mReadAgainButton.setOnClickListener(本);
            }
        }
    }
 

完整的源代码:

 公共类ConfigFinalActivity扩展活动实现OnClickListener {
    私有静态最后字符串变量=ConfigFinalActivity;
    TelephonyManager TM;
    AlertDialog mErrorAlert = NULL;
    民营通知mNotification = NULL;
    私人按钮mXButton = NULL;
    私人ImageView的mAssistUpdateButton = NULL;
    私人按钮mAssistInstrButton = NULL;
    私人TextView的mReadAgainButton = NULL;
    私人诠释mInstructionNumber = 0;
    公共静态的ArrayList<字符串> NameArr =新的ArrayList<字符串>();
    公共静态的ArrayList<字符串> ValueArr =新的ArrayList<字符串>();
    公共静态的ArrayList<字符串> nameArr =新的ArrayList<字符串>();
    公共静态的ArrayList<字符串> ApnArr =新的ArrayList<字符串>();
    公共静态的ArrayList<字符串> mmscArr =新的ArrayList<字符串>();
    公共静态的ArrayList<字符串> mmsportArr =新的ArrayList<字符串>();
    公共静态的ArrayList<字符串> mmsproxyArr =新的ArrayList<字符串>();
    公共静态的ArrayList<字符串> portArr =新的ArrayList<字符串>();
    公共静态的ArrayList<字符串> proxyArr =新的ArrayList<字符串>();
    公共静态诠释计数;
    公共静态INT TotalSteps = 10;
    INT I,G = 0,结果为0;

    公共静态ContentValues​​值=新ContentValues​​();

    公共静态最终乌里APN_TABLE_URI =开放的我们
            .parse(内容://电话/运营公司);
    公共静态的InputStream流= NULL;
    UpdateActivity更新;
    公共静态字符串的地位,QueryResult中=;

    / **第一次创建活动时调用。 * /
    @燮pressLint(NewApi)
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        INT版本= android.os.Build.VERSION.SDK_INT;
        TM =(TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        更新=新UpdateActivity();
        getArrayLists();

        如果(ApnArr.isEmpty()|| mmscArr.isEmpty()){

            再试一次();
        }否则如果(版本< VERSION_ codeS.ICE_CREAM_SANDWICH){

            SettingsDbAdapter.setArrayLists(nameArr,ApnArr,mmscArr,
                    mmsproxyArr,mmsportArr,proxyArr,portArr,计数,
                    ConfigFinalActivity.this);
            //更新APN表
            尝试 {
                结果= updateTable();
            }赶上(IOException异常E1){
                // TODO自动生成的catch块
                e1.printStackTrace();
            } //设置更新了此原子电话
            赶上(的SAXException E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }赶上(的ParserConfigurationException E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }

            如果(结果!=  -  1){
                状态=成功;

            } 其他 {
                状态=失败;
            }

            如果(status.equals(成功)){
                completeUpdate();
            }否则,如果(status.equals(失败)){
                再试一次();

            }

        }其他{// ICS和更高版本

            //减少步骤数,以10
            TotalSteps = 10;
            的setContentView(R.layout.assist_update);
            字符串assistUpdate =的getString(R.string.instructions_1);
            mAssistUpdateButton =(ImageView的)findViewById(R.id.assist_update_btn);
            mAssistUpdateButton.setOnClickListener(本);

        }
    }

    私人无效getArrayLists(){
        nameArr = update.getnameArr();
        ApnArr = update.getApnArr();
        mmscArr = update.getMMSCArr();
        mmsproxyArr = update.getMmscProxyArr();
        mmsportArr = update.getMmsPortArr();
        proxyArr = update.getProxyArr();
        portArr = update.getPortArr();
        数= update.getCount();
        QueryResult中= update.getResult();
    }

    公共无效的onClick(视图v){
        开关(v.getId()){
        案例R.id.assist_update_btn:
            开关(mInstructionNumber){

            }
            //公共无效的onClick(视图v){
            //如果(V == mAssistUpdateButton){

            //被选为ICS及以上更新按钮
            //获取协助更新UI TextView的

            TextView的电视=(TextView中)findViewById(R.id.apn_app_text_cta2);
            字符串文本=;
            CharSequence中的StyledText =文本;
            开关(mInstructionNumber){

            情况下0:
                //获得对应的指令串资源
                //第二个指令集
                文=的String.Format(的getString(R.string.apn_app_text_instr)
                        TotalSteps);
                的StyledText = Html.fromHtml(文本);
                //更新与正确的指令集的TextView的
                tv.setText(的StyledText);
                所以正确的指令//增量指令号
                //串资源可以是检索下一时间的更新
                //按钮是pressed
                mInstructionNumber ++;
                打破;
            情况1:
                文=的getString(R.string.apn_app_text_instr2);
                的StyledText = Html.fromHtml(文本);
                tv.setText(的StyledText);
                所以正确的指令//增量指令号
                //串资源可以是检索下一时间的更新
                //按钮是pressed
                mInstructionNumber ++;
                打破;
            案例2:
                //决胜盘说明,更改为相应的布局

                的setContentView(R.layout.assist_instructions);
                字符串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 =(按钮)findViewById(R.id.assist_instr_btn);
                mAssistInstrButton.setOnClickListener(本);
                mReadAgainButton =(TextView中)findViewById(R.id.read_again_btn);
                mReadAgainButton.setOnClickListener(本);
            }
        //}
    //开关(v.getId()){
        案例R.id.assist_instr_btn:
            startActivity(新意图(Settings.ACTION_APN_SETTINGS));
            尝试 {
                showNotification();
            }赶上(的SAXException E1){
                // TODO自动生成的catch块
                e1.printStackTrace();
            }赶上(E1的ParserConfigurationException){
                // TODO自动生成的catch块
                e1.printStackTrace();
            }
            尝试 {
                showNotification();
            }赶上(的SAXException E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }赶上(的ParserConfigurationException E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }
            完();



// /开关(v.getId()){
        案例R.id.read_again_btn:
            //开关(mInstructionNumber)
            //返回指令集如果选择再读

            TextView的TV2 =(TextView中)findViewById(R.id.apn_app_text_cta2);
        //字符串文本=;
            // CharSequence中的StyledText =文本;
            开关(mInstructionNumber){

            情况下0:
                //获得对应的指令串资源
                //第二个指令集
                文=的String.Format(的getString(R.string.apn_app_text_instr)
                        TotalSteps);
                的StyledText = Html.fromHtml(文本);
                //更新与正确的指令集的TextView的
                tv2.setText(的StyledText);
                所以正确的指令//增量指令号
                //串资源可以是检索下一时间的更新
                //按钮是pressed
                mInstructionNumber ++;
                打破;
            情况1:
                文=的getString(R.string.apn_app_text_instr2);
                的StyledText = Html.fromHtml(文本);
                tv2.setText(的StyledText);
                所以正确的指令//增量指令号
                //串资源可以是检索下一时间的更新
                //按钮是pressed
                mInstructionNumber ++;
                打破;
            案例2:
                //决胜盘说明,更改为相应的布局

                的setContentView(R.layout.assist_instructions);
                字符串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 =(按钮)findViewById(R.id.assist_instr_btn);
                mAssistInstrButton.setOnClickListener(本);
                mReadAgainButton =(TextView中)findViewById(R.id.read_again_btn);
                mReadAgainButton.setOnClickListener(本);
            }
        }
    }

    公共ContentValues​​的GetValues​​(){
        ContentValues​​值=新ContentValues​​();

        如果(nameArr.size()大于0){
            values​​.put(姓名,nameArr.get(ⅰ));
        }
        如果(ApnArr.size()大于0){
            values​​.put(APN,ApnArr.get(I));
        }
        如果(mmscArr.size()大于0){
            values​​.put(MMSC,mmscArr.get(ⅰ));
        }
        如果(mmsproxyArr.size()大于0){
            values​​.put(mmsproxy,mmsproxyArr.get(ⅰ));
        }
        如果(mmsportArr.size()大于0){
            values​​.put(mmsport,mmsportArr.get(ⅰ));
        }
        如果(proxyArr.size()大于0){
            values​​.put(代理,proxyArr.get(ⅰ));
        }
        如果(portArr.size()大于0){
            values​​.put(口,portArr.get(I));
        }
        values​​.put(MCC(的getString(R.string.mcc)));
        如果((tm.getSimOperator())。等于(的getString(R.string.numeric_tmo))){
            values​​.put(数字的getString(R.string.numeric_tmo));
            values​​.put(MNC,(的getString(R.string.mnc_tmo)));
        }否则,如果((tm.getSimOperator())
                .equals(的getString(R.string.numeric_att))){
            values​​.put(数字的getString(R.string.numeric_att));
            values​​.put(MNC,(的getString(R.string.mnc_att)));
        }
        values​​.put(类型,的getString(R.string.type));
        返回值;
    }

    // showAlert显示包含在消息警报文本
    公共无效showAlert(字符串消息){
        AlertDialog.Builder建设者=新AlertDialog.Builder(本);
        builder.setMessage(消息).setPositiveButton(OK,
                新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话框,INT ID){
                ConfigFinalActivity.this.finish();
            }
        });
        mErrorAlert = builder.create();
        mErrorAlert.show();
    }

    // showErrorAlert显示与布局和标题警报
    私人无效showErrorAlert(INT layoutRes,串题){
        AlertDialog.Builder建设者=新AlertDialog.Builder(本);
        //获取布局充气
        LayoutInflater充气= ConfigFinalActivity.this.getLayoutInflater();

        //充气和设置布局的对话
        //传递null作为父视图,因为它在该对话框的布局会
        builder.setTitle(标题)
        .setView(inflater.inflate(layoutRes,NULL))
        .setPositiveButton(的getString(R.string.assisted_button)
                新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话框,INT ID){
                startActivity(新意图(
                        Settings.ACTION_APN_SETTINGS));
                尝试 {
                    showNotification();
                }赶上(的SAXException E){
                    // TODO自动生成的catch块
                    e.printStackTrace();
                }赶上(的ParserConfigurationException E){
                    // TODO自动生成的catch块
                    e.printStackTrace();
                }
            }
        });
        mErrorAlert = builder.create();
        mErrorAlert.show();
    }

    // showNotification开始发送通知条的过程
    //帮助用户在更新的ICS和更高的数据设置
    Android系统//版本
    @燮pressWarnings(德precation)
    @TargetApi(Build.VERSION_ codeS.ICE_CREAM_SANDWICH)
    无效showNotification()抛出的SAXException,的ParserConfigurationException {

        字符串字段=的getString(R.string.config_name_label);

        字符串值= Values​​.get(姓名)的toString()。
        INT MID = 1;
        字符串标题=1+ TotalSteps +(更新+现场+:)
        Notification.Builder mBuilder =新Notification.Builder(本)
        .setSmallIcon(R.drawable.notifications_icon)
        .setContentTitle(标题).setContentText(值);
        意图resultIntent =新的意图(这一点,
                NotificationActivityForMultiProf.class);
        resultIntent.putExtra(场,值);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(
                getApplicationContext(),0,resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        // MID可以让你以后更新通知。
        mNotification = mBuilder.getNotification();
        mNotification.flags | = Notification.FLAG_AUTO_CANCEL;
        mNotificationManager.notify(MID,mNotification);
        完();
    }

    @覆盖
    保护无效的onSaveInstanceState(包outState){
        // TODO自动生成方法存根
        super.onSaveInstanceState(outState);
        如果(mNotification!= NULL){
            outState.putString(NOTIFICATIONB,mNotification.toString());
        }
    }

    @覆盖
    保护无效onRestart(){
        super.onRestart();
        如果(mErrorAlert!= NULL)
            mErrorAlert.dismiss();
    }

    私人诠释updateTable()抛出IOException异常,SAXException中,
    的ParserConfigurationException {
        INT insertResult = -1;
        //返回值,如果表是不正确地更新

        尝试 {

            @燮pressWarnings(未使用)
            ContentValues​​值=新ContentValues​​();
            //查询载体表中为当前的数据设定
            光标C = getContentResolver()查询(APN_TABLE_URI,空,
                    当前=?,新的String [] {1},NULL);

            值= SettingsDbAdapter.copyRecordFields(C);
            //复制NET10设置成值

            //替换T-MO / ATT数据设置,如果没有SIM卡或
            // StraightTalk / T-MO / ATT SIM卡是
            // present
            如果(tm.getSimState()== TelephonyManager.SIM_STATE_ABSENT
                    || (tm.getSimOperator())
                    .equals(的getString(R.string.numeric_tmo))){

                //添加新的APN之前删除所有的APN

                SettingsDbAdapter.DeleteAPNs(数字=?,
                        新的String [] {的getString(R.string.numeric_att)},
                        ConfigFinalActivity.this);
                //插入StraightTalk数据设置成载波表

                insertResult = SettingsDbAdapter.InsertAPN(计数,
                        ConfigFinalActivity.this);

            }否则,如果(tm.getSimState()== TelephonyManager.SIM_STATE_ABSENT
                    || (tm.getSimOperator())
                    .equals(的getString(R.string.numeric_att))){
                //添加新的APN之前删除所有的APN

                SettingsDbAdapter.DeleteAPNs(数字=?,
                        新的String [] {的getString(R.string.numeric_att)},
                        ConfigFinalActivity.this);
                //将实话实说数据设置成载波表

                insertResult = SettingsDbAdapter.InsertAPN(计数,
                        ConfigFinalActivity.this);

            } 其他
                //非实话实说/非T型钼SIM /非AT&T SIM卡
                showAlert(的getString(R.string.insert_sm_dialog));
        }赶上(SecurityException异常E){
            showErrorAlert(R.layout.assisted_setting,
                    的getString(R.string.assited_title));
            Log.d(TAG,e.getMessage());
        }
        返回insertResult;
    }

    私人无效completeUpdate(){
        //显示pre-ICS自动设置更新后的最终布局
        的setContentView(R.layout.completion);
        TextView的mCompleted =(TextView中)findViewById(R.id.done_text);
        字符串mDoneText =的String.Format(的getString(R.string.done_text));
        CharSequence中的StyledText = Html.fromHtml(mDoneText);

        mCompleted.setText(的StyledText);
        mXButton =(按钮)findViewById(R.id.x_button);
        mXButton.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                完();
            }
        });
    }

    公共无效TRYAGAIN(){
        //显示最终布局的pre-ICS自动设置失败后
        //更新
        的setContentView(R.layout.tryagain);
        字符串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);

    }

    //这个函数返回一个指针表持有
    //的APN配置(载波表)
    公共光标getConfigTableCursor(){
        返回getContentResolver()
                .query(APN_TABLE_URI,NULL,NULL,NULL,NULL);
    }

}
 

解决方案

这是一个坏主意,用 == 运营商比较对象。尝试像这样:

 公共无效的onClick(视图v){
    开关(v.getId()){
        案例R.id.yourButton:
            开关(mInstructionNumber){
                情况下0:
                情况1:
                ...
            }
        案例R.id.ofmAssistInstrButton:
        案例R.id.ofmAssistInstrButton://我想你犯了一个错字在这里,因为他们是同一个名字
        默认:
            //你else语句的东西
        ...
    }
}
 

I have an onClickListener set on a TextView (mReadAgainButton) which is supposed to reload a set of instructions from the beginning (initially displayed via an onClickListener on a Button called mAssistUpdateButton) however when clicking on the TextView mReadAgainButton nothing seems to happen.

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);
            }
        }
    }

FULL SOURCE:

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);
    }

}

解决方案

It is a bad idea to compare objects with the == operator. Try something like this instead:

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
        ...
    }
}

这篇关于onClick的监听器无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆