调试程序报错空指针,求广大网友帮忙看看怎么回事
MainActivity.java的部分代码:
public void initView() {
title = (TextView) findViewById(R.id.title);
phonenumber = (TextView) findViewById(R.id.phonenumbertxt);
helpinfotext = (TextView) findViewById(R.id.helpinfotext);
selectCompanyLayout = (LinearLayout) findViewById(R.id.companyname_layout);
companynameTxt = (TextView) findViewById(R.id.companynametxt);
companynameTxt.setText("");//**********这里报空指针错误*********
getNumberLayout = (LinearLayout) findViewById(R.id.number_layout);
order_numTxt = (EditText) findViewById(R.id.order_numtxt);
searchBtn = (Button) findViewById(R.id.searchbtn);
historyBtn = (Button) findViewById(R.id.historybtn);
selectCompanyLayout.setOnClickListener(this);
searchBtn.setOnClickListener(this);
historyBtn.setOnClickListener(this);
progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage("正在为您查询...");//664872754739
progressDialog.setCancelable(true);
progressDialog.setTitle("提示信息");
changeBtnBackground();
if (!Common.hasShortcut(this)) { // 创建快捷方式
Common.addShortcut(this);
}
if (!Common.onlineFlag(MainActivity.this)) {
Toast.makeText(this, "离线使用中,非联网状态不能查询订单信息!", Toast.LENGTH_SHORT)
.show();
}
}
Logcat中的记录:
Process: com.example.viewpager, PID: 14743
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.viewpager/com.checkpackage.checkpackage.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2505)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2577)
at android.app.ActivityThread.access$1000(ActivityThread.java:164)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1462)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:159)
at android.app.ActivityThread.main(ActivityThread.java:5541)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.checkpackage.checkpackage.MainActivity.initView(MainActivity.java:86)
at com.checkpackage.checkpackage.MainActivity.onCreate(MainActivity.java:70)
at android.app.Activity.performCreate(Activity.java:6107)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2458)
????????????at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2577)
????????????at android.app.ActivityThread.access$1000(ActivityThread.java:164)
????????????at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1462)
????????????at android.os.Handler.dispatchMessage(Handler.java:102)
????????????at android.os.Looper.loop(Looper.java:159)
????????????at android.app.ActivityThread.main(ActivityThread.java:5541)
????????????at java.lang.reflect.Method.invoke(Native Method)
????????????at java.lang.reflect.Method.invoke(Method.java:372)
????????????at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
????????????at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
MainActivity.java的部分代码:
public void initView() {
title = (TextView) findViewById(R.id.title);
phonenumber = (TextView) findViewById(R.id.phonenumbertxt);
helpinfotext = (TextView) findViewById(R.id.helpinfotext);
selectCompanyLayout = (LinearLayout) findViewById(R.id.companyname_layout);
companynameTxt = (TextView) findViewById(R.id.companynametxt);
companynameTxt.setText("");//**********这里报空指针错误*********
getNumberLayout = (LinearLayout) findViewById(R.id.number_layout);
order_numTxt = (EditText) findViewById(R.id.order_numtxt);
searchBtn = (Button) findViewById(R.id.searchbtn);
historyBtn = (Button) findViewById(R.id.historybtn);
selectCompanyLayout.setOnClickListener(this);
searchBtn.setOnClickListener(this);
historyBtn.setOnClickListener(this);
progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage("正在为您查询...");//664872754739
progressDialog.setCancelable(true);
progressDialog.setTitle("提示信息");
changeBtnBackground();
if (!Common.hasShortcut(this)) { // 创建快捷方式
Common.addShortcut(this);
}
if (!Common.onlineFlag(MainActivity.this)) {
Toast.makeText(this, "离线使用中,非联网状态不能查询订单信息!", Toast.LENGTH_SHORT)
.show();
}
}
Logcat中的记录:
Process: com.example.viewpager, PID: 14743
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.viewpager/com.checkpackage.checkpackage.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2505)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2577)
at android.app.ActivityThread.access$1000(ActivityThread.java:164)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1462)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:159)
at android.app.ActivityThread.main(ActivityThread.java:5541)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.checkpackage.checkpackage.MainActivity.initView(MainActivity.java:86)
at com.checkpackage.checkpackage.MainActivity.onCreate(MainActivity.java:70)
at android.app.Activity.performCreate(Activity.java:6107)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2458)
????????????at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2577)
????????????at android.app.ActivityThread.access$1000(ActivityThread.java:164)
????????????at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1462)
????????????at android.os.Handler.dispatchMessage(Handler.java:102)
????????????at android.os.Looper.loop(Looper.java:159)
????????????at android.app.ActivityThread.main(ActivityThread.java:5541)
????????????at java.lang.reflect.Method.invoke(Native Method)
????????????at java.lang.reflect.Method.invoke(Method.java:372)
????????????at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
????????????at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)