Wifi启用代码中的java.lang.NullPointerException

时间:2014-09-18 12:50:11

标签: android android-activity nullpointerexception

我正在编写用于启用wifi的简单代码。代码在此处显示NUllPointerExcepation。任何人都可以帮助我。这些代码工作得更早,但现在却没有。不知道我在做什么错。

这是代码:

TurnOnWIfiTestAction类:

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;

public class TurnOnWIfiTestAction {

    private  WifiManager wifi_manager;
    private  WifiConfiguration wifi_config;
    private Context paramContext;

    public TurnOnWIfiTestAction() {

    }

    public void turnOnWifi() throws Exception {
         wifi_manager = (WifiManager) paramContext.getSystemService(Context.WIFI_SERVICE);
         wifi_manager.setWifiEnabled(true);
         Thread.sleep(2000L);

    }

}

MainActivity

import com.wifiTest.TurnOnWIfiTestAction;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    public void wifienable (View view)
    {
        TurnOnWIfiTestAction wifinOn = new TurnOnWIfiTestAction();

        try {
            wifinOn.turnOnWifi();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

记录:

01-01 22:53:40.994: W/System.err(13438): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
01-01 22:53:40.994: W/System.err(13438):    at com.wifiTest.TurnOnWIfiTestAction.turnOnWifi(TurnOnWIfiTestAction.java:18)
01-01 22:53:40.994: W/System.err(13438):    at com.post.l_upgrade.MainActivity.wifienable(MainActivity.java:46)
01-01 22:53:40.994: W/System.err(13438):    at java.lang.reflect.Method.invoke(Native Method)
01-01 22:53:40.994: W/System.err(13438):    at java.lang.reflect.Method.invoke(Method.java:372)
01-01 22:53:40.994: W/System.err(13438):    at android.view.View$1.onClick(View.java:3986)
01-01 22:53:41.000: W/System.err(13438):    at android.view.View.performClick(View.java:4728)
01-01 22:53:41.000: W/System.err(13438):    at android.view.View$PerformClick.run(View.java:19508)
01-01 22:53:41.000: W/System.err(13438):    at android.os.Handler.handleCallback(Handler.java:739)
01-01 22:53:41.002: W/System.err(13438):    at android.os.Handler.dispatchMessage(Handler.java:95)
01-01 22:53:41.002: W/System.err(13438):    at android.os.Looper.loop(Looper.java:135)
01-01 22:53:41.004: W/System.err(13438):    at android.app.ActivityThread.main(ActivityThread.java:5212)
01-01 22:53:41.004: W/System.err(13438):    at java.lang.reflect.Method.invoke(Native Method)
01-01 22:53:41.004: W/System.err(13438):    at java.lang.reflect.Method.invoke(Method.java:372)
01-01 22:53:41.004: W/System.err(13438):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
01-01 22:53:41.006: W/System.err(13438):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:686)
01-01 22:53:41.186: E/AudioPolicyManager(223): unknown stream type

2 个答案:

答案 0 :(得分:3)

您的paramContext=null

 wifi_manager = (WifiManager) paramContext.getSystemService(Context.WIFI_SERVICE);

所以,你得到了NPE。

修正:

在构造函数中传递context并按如下所示进行初始化

 public TurnOnWIfiTestAction(Context con) {
   this.paramContext=con;
  }

答案 1 :(得分:0)

turnOnWifi()

中传递turnOnWifi(context)中的上下文,例如MainActivity

更新

public void turnOnWifi(Context context) throws Exception {
     wifi_manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
     wifi_manager.setWifiEnabled(true);
     Thread.sleep(2000L);
}

注意:如果使用static方法,则无需创建Class对象。所以你可以直接调用

TurnOnWIfiTestAction.turnOnWifi(context);