NullPointerException异常引起的:task.execute(""); [英] NullPointerException Caused by: task.execute("");

查看:91
本文介绍了NullPointerException异常引起的:task.execute("");的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我执行我的应用程序时,得到一个空指针异常,我不知道为什么。上线出现该问题:

  task.execute();
 

但我不知道为什么会回来跟一个空指针异常。 (我已经验证我有互联网连接,它是连接是否完好和功能的XML文件。)

来源:

 公共类UpdateActivity扩展活动实现OnClickListener {

    公共静态INT TotalSteps = 8;
    私人TelephonyManager TM;

    私人按钮mUpdateButton = NULL;
    私人按钮mAssistUpdateButton = NULL;
    私人按钮mAssistInstrButton = NULL;
    私人TextView的mReadAgainButton = NULL;
    私人诠释mInstructionNumber = 0;
    AlertDialog mConfirmAlert = NULL;
    私人NetworkTask任务;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        TM =(TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        INT NETWORKTYPE = tm.getNetworkType();
        INT phoneType = tm.getPhoneType();
        INT版本= android.os.Build.VERSION.SDK_INT;
        如果(phoneType == TelephonyManager.PHONE_TYPE_CDMA
                || (phoneType!= TelephonyManager.PHONE_TYPE_GSM
                &功放;&安培; NETWORKTYPE!= TelephonyManager.NETWORK_TYPE_GPRS
                &功放;&安培; NETWORKTYPE!= TelephonyManager.NETWORK_TYPE_EDGE
                &功放;&安培; NETWORKTYPE!= TelephonyManager.NETWORK_TYPE_HSDPA
                &功放;&安培; NETWORKTYPE!= TelephonyManager.NETWORK_TYPE_HSPA
                &功放;&安培; NETWORKTYPE!= TelephonyManager.NETWORK_TYPE_HSPAP
                &功放;&安培; NETWORKTYPE!= TelephonyManager.NETWORK_TYPE_HSUPA
                &功放;&安培; NETWORKTYPE = TelephonyManager.NETWORK_TYPE_UMTS和放大器;!&安培; NETWORKTYPE!= TelephonyManager.NETWORK_TYPE_LTE)){
            //如果手机类型为CDMA或
            //电话手机类型不是GSM和网络类型是没有
            //网络类型在声明中表示
            //显示不兼容性消息
            showAlert(的getString(R.string.incomp_sm_dialog));
            //网络类型看,因为一些药片没有手机类型。
            //我们依靠在这种情况下,网络类型
        }否则,如果(!(tm.getSimState()== TelephonyManager.SIM_STATE_ABSENT
                || (tm.getSimOperator())
                .equals(的getString(R.string.numeric_tmo))|| (TM
                        .getSimOperator())。等于(的getString(R.string.numeric_att)))){
            //如果SIM卡是present,不是T型钼网SIM卡,
            //显示错误信息提示,指示使用SM SIM卡
            showAlert(的getString(R.string.insert_sm_dialog));
        } //没有SIM卡或SIM卡与T-莫MNC MCC present
        否则,如果(版本< VERSION_ codeS.ICE_CREAM_SANDWICH){
            比ICS //初始UI安装的版本更低
            的setContentView(R.layout.update);
            mUpdateButton =(按钮)findViewById(R.id.update_button);

            mUpdateButton.setOnClickListener(本);
        }其他{// ICS及以上

            // task.execute();

            如果((tm.getSimOperator())。等于(的getString(R.string.numeric_tmo))
                    || (tm.getSimOperator())
                    .equals(的getString(R.string.numeric_att))){
                task.execute();
                //设备具有T型钼网络的SIM卡MCC和MNC正确
                //人口
                //减少步骤数,以6
                TotalSteps = 6;
            }
            //
            //初始UI设置为ICS及以上

            的setContentView(R.layout.assist_update);
            字符串assistUpdate =的getString(R.string.apn_app_text_cta2);
            CharSequence中的StyledText = Html.fromHtml(assistUpdate);
            TextView的assistText =(TextView中)findViewById(R.id.apn_app_text_cta2);
            assistText.setText(的StyledText);
            mAssistUpdateButton =(按钮)findViewById(R.id.assist_update_btn);
            mAssistUpdateButton.setOnClickListener(本);
        }
    }

    公共无效的onClick(视图v){

        如果(V == mUpdateButton){
            //被选为版本比ICS更低更新按钮
            //的setContentView(R.layout.updating);
            onClickMethod(五);

            意图I =新的意图(这一点,ConfigFinalActivity.class);
            startActivity(ⅰ);
            完();
        }否则,如果(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);
                mReadAgainButton =(TextView中)findViewById(R.id.read_again_btn);
                mAssistInstrButton.setOnClickListener(本);
                mReadAgainButton.setOnClickListener(本);
            }
        }否则,如果(V == mAssistInstrButton){
            //LET'S做到这一点按钮,在最后指示屏幕ICS和
            //中选择了
            //创建ConfigActivity意向
            意图I =新的意图(这一点,ConfigFinalActivity.class);
            //调用ConfigActivity意图,开始协助更新
            startActivity(ⅰ);
            完();
        }否则,如果(V == mReadAgainButton){
            //返回指令1套,如果选择再次读取
            mInstructionNumber = 0;
            的setContentView(R.layout.assist_update);
            字符串assistUpdate =的getString(R.string.apn_app_text_cta2);
            CharSequence中的StyledText = Html.fromHtml(assistUpdate);
            TextView的assistText =(TextView中)findViewById(R.id.apn_app_text_cta2);
            assistText.setText(的StyledText);
            mAssistUpdateButton =(按钮)findViewById(R.id.assist_update_btn);
            mAssistUpdateButton.setOnClickListener(本);
        }
    }

    公共无效onClickMethod(视图v){
        mUpdateButton =(按钮)findViewById(R.drawable.btn_update_active_hdpi);

    }

    私人无效showAlert(字符串消息){
        AlertDialog.Builder建设者=新AlertDialog.Builder(本);
        builder.setMessage(消息).setPositiveButton(OK,
                新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话框,INT ID){
                UpdateActivity.this.finish();
            }
        });
        mConfirmAlert = builder.create();
        mConfirmAlert.show();
    }

}
 

解决方案

据我所知,你永远不会初始化工作。您在这里声明它

 私人NetworkTask任务;
 

但你需要调用执行前初始化()。像

 任务=新NetworkTask(); //传递PARAMS构造器,如果需要
task.execute();
 

I'm getting a null pointer exception when executing my app and I'm not sure why. The issue occurs on the line:

    task.execute("");

but I'm not sure why this would come back with a null pointer exception. (I've verified I have internet connectivity and the XML file it is connecting to is intact and functional.)

SOURCE:

public class UpdateActivity extends Activity implements OnClickListener {

    public static int TotalSteps = 8;
    private TelephonyManager tm;

    private Button mUpdateButton = null;
    private Button mAssistUpdateButton = null;
    private Button mAssistInstrButton = null;
    private TextView mReadAgainButton = null;
    private int mInstructionNumber = 0;
    AlertDialog mConfirmAlert = null;
    private NetworkTask task;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        int networkType = tm.getNetworkType();
        int phoneType = tm.getPhoneType();
        int version = android.os.Build.VERSION.SDK_INT;
        if (phoneType == TelephonyManager.PHONE_TYPE_CDMA
                || (phoneType != TelephonyManager.PHONE_TYPE_GSM
                && networkType != TelephonyManager.NETWORK_TYPE_GPRS
                && networkType != TelephonyManager.NETWORK_TYPE_EDGE
                && networkType != TelephonyManager.NETWORK_TYPE_HSDPA
                && networkType != TelephonyManager.NETWORK_TYPE_HSPA
                && networkType != TelephonyManager.NETWORK_TYPE_HSPAP
                && networkType != TelephonyManager.NETWORK_TYPE_HSUPA
                && networkType != TelephonyManager.NETWORK_TYPE_UMTS && networkType != TelephonyManager.NETWORK_TYPE_LTE)) {
            // If the phone type is CDMA or
            // the phone phone type is not GSM and the network type is none of
            // the network types indicated in the statement
            // Display incompatibility message
            showAlert(getString(R.string.incomp_sm_dialog));
            // Network type is looked because some tablets have no phone type.
            // We rely on network type in such cases
        } else if (!(tm.getSimState() == TelephonyManager.SIM_STATE_ABSENT
                || (tm.getSimOperator())
                .equals(getString(R.string.numeric_tmo)) || (tm
                        .getSimOperator()).equals(getString(R.string.numeric_att)))) {
            // if SIM is present and is NOT a T-Mo network SIM,
            // display Error message alert indicating to use SM SIM
            showAlert(getString(R.string.insert_sm_dialog));
        }// No SIM or SIM with T-Mo MNC MCC present
        else if (version < VERSION_CODES.ICE_CREAM_SANDWICH) {
            // Initial UI setup for versions lower than ICS
            setContentView(R.layout.update);
            mUpdateButton = (Button) findViewById(R.id.update_button);

            mUpdateButton.setOnClickListener(this);
        } else {// ICS and up

            // task.execute();

            if ((tm.getSimOperator()).equals(getString(R.string.numeric_tmo))
                    || (tm.getSimOperator())
                    .equals(getString(R.string.numeric_att))) {
                task.execute("");
                // Device has T-Mo network SIM card MCC and MNC correctly
                // populated
                // Reduce number of steps to 6
                TotalSteps = 6;
            }
            //
            // Initial UI setup for ICS and up

            setContentView(R.layout.assist_update);
            String assistUpdate = getString(R.string.apn_app_text_cta2);
            CharSequence styledText = Html.fromHtml(assistUpdate);
            TextView assistText = (TextView) findViewById(R.id.apn_app_text_cta2);
            assistText.setText(styledText);
            mAssistUpdateButton = (Button) findViewById(R.id.assist_update_btn);
            mAssistUpdateButton.setOnClickListener(this);
        }
    }

    public void onClick(View v) {

        if (v == mUpdateButton) {
            // Update button for versions lower than ICS is selected
            // setContentView(R.layout.updating);
            onClickMethod(v);

            Intent i = new Intent(this, ConfigFinalActivity.class);
            startActivity(i);
            finish();
        } else 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);
                mReadAgainButton = (TextView) findViewById(R.id.read_again_btn);
                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
            // Create ConfigActivity Intent
            Intent i = new Intent(this, ConfigFinalActivity.class);
            // Invoke ConfigActivity Intent to start the assisted update
            startActivity(i);
            finish();
        } else if (v == mReadAgainButton) {
            // go back to 1st set of instructions if read again is selected
            mInstructionNumber = 0;
            setContentView(R.layout.assist_update);
            String assistUpdate = getString(R.string.apn_app_text_cta2);
            CharSequence styledText = Html.fromHtml(assistUpdate);
            TextView assistText = (TextView) findViewById(R.id.apn_app_text_cta2);
            assistText.setText(styledText);
            mAssistUpdateButton = (Button) findViewById(R.id.assist_update_btn);
            mAssistUpdateButton.setOnClickListener(this);
        }
    }

    public void onClickMethod(View v) {
        mUpdateButton = (Button) findViewById(R.drawable.btn_update_active_hdpi);

    }

    private 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) {
                UpdateActivity.this.finish();
            }
        });
        mConfirmAlert = builder.create();
        mConfirmAlert.show();
    }

}

解决方案

As far as I can tell, you never initialize task. You declare it here

private NetworkTask task;

but you need to initialize before calling execute(). Like

task = new NetworkTask();  // pass params to constructor if needed
task.execute();

这篇关于NullPointerException异常引起的:task.execute(&QUOT;&QUOT;);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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