java - NullPointerException 由 : task. 执行 (""引起);

标签 java android nullpointerexception fatal-error

我在执行应用程序时遇到空指针异常,但我不确定原因。问题发生在线路上:

    task.execute("");

但我不确定为什么会出现空指针异常。 (我已经验证我有互联网连接,并且它所连接的 XML 文件完好无损且功能正常。)

来源:

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

}

最佳答案

据我所知,您永远不会初始化任务。您在这里声明

private NetworkTask task;

但您需要在调用execute()之前进行初始化。喜欢

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

关于java - NullPointerException 由 : task. 执行 (""引起);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18062586/

相关文章:

java - 我正在使用 Actions 类移动到某个元素,但无法单击下拉列表中的元素

java - 如何在eclipse中为java代码添加快捷键

android - Google Inapp 购买和试用期

JavaFX 目录选择器 - 如何从目录获取路径?

java - 如何用java制作沙盒游戏

java - 从 CentOS 7 终端运行 java 时出现 ClassNotFound 异常

java - JPQL SELECT 日期语句之间

EditText 中的 Android 标签或气泡

java - 重用以编程方式创建的 TextView

android - SQLiteOpenHelper.getWritableDatabase() 抛出 NullPointerException ORMLite