描述:在Mainactivity中实例化一个handler = new Handler(),然后全局化过程中出现了空指针错误,myApp.sethandler(handler)报空指针错误
//MainActivity.java
package com.wanmen.acer.wifi_test;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public static int MSG = 1;
MyApp myApp = (MyApp) getApplication();
//myApp.sethandler(handler);
private Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what == 1)
{
Toast.makeText(getApplicationContext(),”get handler msg!!!”,Toast.LENGTH_SHORT).show();
}
}
};
Button Check =null;
public Button mainbtn = null;
public String wifiname = null;
public String wifiMac = null;
public int wifiID ;
public int wifiIP ;
private WifiUtils localwifi;
public void dialog(String str)
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage(str);
builder.setTitle(“提示”);
builder.setPositiveButton(“ok”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
builder.create().show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myApp.sethandler(handler);//为什么要放在oncreat方法内?空指针错误
localwifi = new WifiUtils(MainActivity.this);
Check = (Button)findViewById(R.id.check);
mainbtn = (Button)findViewById(R.id.MainButton);
Check.setOnClickListener(listener);
mainbtn.setOnClickListener(listener);
}
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId())
{
case R.id.check:
localwifi.getConnectedInfo();
wifiname = localwifi.getConnectedSSID();
wifiID = localwifi.getConnectedID();
wifiMac = localwifi.getConnectedMacAddr();
wifiIP = localwifi.getConnectedIPAddr();
dialog(“wifi名称:”+wifiname+”\n”+”wifiID:”+wifiID+”\n”+”wifiIP:”+wifiIP+”\n”+”wifiMac:”+wifiMac);
break;
case R.id.MainButton:
Intent intent = new Intent();
intent.setClass(MainActivity.this,SecondActivity.class);
startActivity(intent);
break;
}
}
};
}
//MyApp.java
package com.wanmen.acer.wifi_test;
import android.app.Application;
import android.os.Handler;
/**
* Created by acer on 2016/9/16.
*/
public class MyApp extends Application {
private Handler apphandler = null;
public void sethandler(Handler handler)
{
this.apphandler = handler;
}
public Handler gethandler()
{
return this.apphandler;
}
}
//SecondActivity.java
package com.wanmen.acer.wifi_test;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
/**
* Created by acer on 2016/9/16.
*/
public class SecondActivity extends Activity {
public static int MSG = 1;
private MyApp myApp = null;
private Handler handler = null;
private Button button1 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sec);
myApp = (MyApp)getApplication();
handler = myApp.gethandler();
button1 = (Button)findViewById(R.id.send);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
handler.sendEmptyMessage(MSG);
SecondActivity.this.finish();
}
});
}
}
logcat:
09-16 17:46:06.336 3425-3425/com.wanmen.acer.wifi_test E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.wanmen.acer.wifi_test, PID: 3425
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wanmen.acer.wifi_test/com.wanmen.acer.wifi_test.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5309)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:831)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.wanmen.acer.wifi_test.MainActivity.onCreate(MainActivity.java:68)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5309)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:831)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647)
at dalvik.system.NativeStart.main(Native Method)
//MainActivity.java
package com.wanmen.acer.wifi_test;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public static int MSG = 1;
MyApp myApp = (MyApp) getApplication();
//myApp.sethandler(handler);
private Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what == 1)
{
Toast.makeText(getApplicationContext(),”get handler msg!!!”,Toast.LENGTH_SHORT).show();
}
}
};
Button Check =null;
public Button mainbtn = null;
public String wifiname = null;
public String wifiMac = null;
public int wifiID ;
public int wifiIP ;
private WifiUtils localwifi;
public void dialog(String str)
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage(str);
builder.setTitle(“提示”);
builder.setPositiveButton(“ok”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
builder.create().show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myApp.sethandler(handler);//为什么要放在oncreat方法内?空指针错误
localwifi = new WifiUtils(MainActivity.this);
Check = (Button)findViewById(R.id.check);
mainbtn = (Button)findViewById(R.id.MainButton);
Check.setOnClickListener(listener);
mainbtn.setOnClickListener(listener);
}
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId())
{
case R.id.check:
localwifi.getConnectedInfo();
wifiname = localwifi.getConnectedSSID();
wifiID = localwifi.getConnectedID();
wifiMac = localwifi.getConnectedMacAddr();
wifiIP = localwifi.getConnectedIPAddr();
dialog(“wifi名称:”+wifiname+”\n”+”wifiID:”+wifiID+”\n”+”wifiIP:”+wifiIP+”\n”+”wifiMac:”+wifiMac);
break;
case R.id.MainButton:
Intent intent = new Intent();
intent.setClass(MainActivity.this,SecondActivity.class);
startActivity(intent);
break;
}
}
};
}
//MyApp.java
package com.wanmen.acer.wifi_test;
import android.app.Application;
import android.os.Handler;
/**
* Created by acer on 2016/9/16.
*/
public class MyApp extends Application {
private Handler apphandler = null;
public void sethandler(Handler handler)
{
this.apphandler = handler;
}
public Handler gethandler()
{
return this.apphandler;
}
}
//SecondActivity.java
package com.wanmen.acer.wifi_test;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
/**
* Created by acer on 2016/9/16.
*/
public class SecondActivity extends Activity {
public static int MSG = 1;
private MyApp myApp = null;
private Handler handler = null;
private Button button1 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sec);
myApp = (MyApp)getApplication();
handler = myApp.gethandler();
button1 = (Button)findViewById(R.id.send);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
handler.sendEmptyMessage(MSG);
SecondActivity.this.finish();
}
});
}
}
logcat:
09-16 17:46:06.336 3425-3425/com.wanmen.acer.wifi_test E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.wanmen.acer.wifi_test, PID: 3425
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wanmen.acer.wifi_test/com.wanmen.acer.wifi_test.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5309)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:831)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.wanmen.acer.wifi_test.MainActivity.onCreate(MainActivity.java:68)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5309)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:831)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647)
at dalvik.system.NativeStart.main(Native Method)
解决方案
20
在setHandler之前一定是还没有实例化handler,没实例化handler是原因是:还没有sendMessage吗?你可以试一下:在sendMessage后在实例化handler看看吧