Java 由以下原因引起的NullPointerException:task.execute(";);
我在执行我的应用程序时遇到空指针异常,我不知道为什么。问题发生在以下线路上:Java 由以下原因引起的NullPointerException:task.execute(";);,java,android,nullpointerexception,fatal-error,Java,Android,Nullpointerexception,Fatal Error,我在执行我的应用程序时遇到空指针异常,我不知道为什么。问题发生在以下线路上: task.execute(""); 但我不确定为什么会出现空指针异常。(我已验证我具有internet连接,并且它连接到的XML文件完好无损且功能正常。) 资料来源: public class UpdateActivity extends Activity implements OnClickListener { public static int TotalSteps = 8; priva
task.execute("");
但我不确定为什么会出现空指针异常。(我已验证我具有internet连接,并且它连接到的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();
}
}
公共类UpdateActivity扩展了活动实现OnClickListener{
公共静态int TotalSteps=8;
专用电话管理器tm;
私有按钮mUpdateButton=null;
private Button-DateButton=null;
私有按钮mAssistInstrButton=null;
私有文本视图mReadAgainButton=null;
私有int mInstructionNumber=0;
AlertDialog mConfirmAlert=null;
专用网络任务;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
tm=(电话管理器)getSystemService(电话服务);
int networkType=tm.getNetworkType();
int phoneType=tm.getPhoneType();
int version=android.os.Build.version.SDK\u int;
if(phoneType==TelephonyManager.PHONE\u TYPE\u CDMA
||(phoneType!=TelephonyManager.PHONE\u TYPE\u GSM
&&networkType!=TelephonyManager.NETWORK\u TYPE\u GPRS
&&networkType!=TelephonyManager.NETWORK\u TYPE\u EDGE
&&networkType!=TelephonyManager.NETWORK\u TYPE\u HSDPA
&&networkType!=TelephonyManager.NETWORK\u TYPE\u HSPA
&&networkType!=TelephonyManager.NETWORK\u TYPE\u HSPAP
&&networkType!=TelephonyManager.NETWORK\u TYPE\u HSUPA
&&networkType!=TelephonyManager.NETWORK\u TYPE\u UMTS&&networkType!=TelephonyManager.NETWORK\u TYPE\u LTE)){
//如果电话类型为CDMA或
//电话类型不是GSM,网络类型不是
//声明中指示的网络类型
//显示不兼容消息
showAlert(getString(R.string.incomp_sm_对话框));
//查看网络类型是因为某些平板电脑没有电话类型。
//在这种情况下,我们依赖于网络类型
}如果(!(tm.getSimState()==TelephonyManager.SIM_STATE_缺席,则为else
||(tm.getSimOperator())
.equals(getString(R.string.numeric_tmo))| |(tm
.getSimOperator()).equals(getString(R.string.numeric_att))){
//如果SIM卡存在且不是T-Mo网络SIM卡,
//显示错误消息警报,指示使用SM SIM卡
showAlert(getString(R.string.insert_sm_对话框));
}//没有SIM卡或存在T-Mo跨国公司MCC的SIM卡
否则如果(版本<版本代码.冰淇淋\u三明治){
//低于ICS版本的初始UI设置
setContentView(R.layout.update);
mUpdateButton=(按钮)findViewById(R.id.update_按钮);
mUpdateButton.setOnClickListener(此);
}else{//ICS及以上
//task.execute();
if((tm.getSimOperator()).equals(getString(R.string.numeric_tmo))
||(tm.getSimOperator())
.equals(getString(R.string.numeric_att))){
任务。执行(“”);
//设备正确安装了T-Mo网络SIM卡MCC和MNC
//人口
//将步骤数减少到6个
总步数=6;
}
//
//ICS及以上的初始UI设置
setContentView(R.layout.assist_更新);
String assistUpdate=getString(R.String.apn\u app\u text\u cta2);
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 update\u btn);
mAssistUpdateButton.setOnClickListener(此);
}
}
公共void onClick(视图v){
if(v==mUpdateButton){
//选择低于ICS版本的更新按钮
//setContentView(R.layout.update);
方法(五);
意向i=新意向(此,configfinalivity.class);
星触觉(i);
完成();
}else if(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(右布局、辅助指示);
private NetworkTask task;
task = new NetworkTask(); // pass params to constructor if needed
task.execute();