Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempt to invoke virtual method 'android.os.Looper android.content.Context.getMainLooper()' on a null object reference [duplicate]

Tags:

java

android

wifi

I am getting this error in my Log cat whenever I try to open the application on either my phone or the emulator. To give you an overview of the project that I am currently doing, it is a system that records the data of the devices connected to the access point on the phone which can be toggled on and off via a button on screen.

I want to give credit to:

  1. Android 2.3 wifi hotspot API

and

  1. https://www.whitebyte.info/android/android-wifi-hotspot-manager-class

an image of the Log cat file is: https://i.gyazo.com/fa068fd1fce3f27f43185c0cd12568c1.png

my main activity class

public class MainActivity extends ActionBarActivity{

boolean wasApEnabled = false;
static AccessPoint wifiAP;
private WifiManager wifi;
static Button apButton;
static TextView textView;

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

    apButton = (Button) findViewById(R.id.toggleBtn);
    textView = (TextView) findViewById(R.id.wifiClients);

    wifiAP = new AccessPoint();
    wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    scan();

    apButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            wifiAP.toggleWifiAP(wifi, MainActivity.this);
        }
    });

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|WindowManager.LayoutParams.FLAG_DIM_BEHIND);

}

private void scan(){
    wifiAP.getClientList(false, new FinishScanListener() {
        @Override
        public void onFinishScan(final ArrayList<ClientScanResult> clients) {
            textView.setText("WifiApState:" + wifiAP.getWifiApState()+ "\n\n");
            textView.append("Clients: \n");
            for (ClientScanResult clientScanResult : clients){
                textView.append("====================\n");
                textView.append("ipAddress: " + clientScanResult.getIpAddress() + "\n");
                textView.append("Device: " + clientScanResult.getDevice() + "\n");
                textView.append("macAddress: " + clientScanResult.getMacAddress() + "\n");
                textView.append("isReachable: " + clientScanResult.isReachable() + "\n");

            }
        }
    });
}

@Override
public void onResume() {
    super.onResume();
    if (wasApEnabled) {
        if (wifiAP.getWifiApState() != wifiAP.WIFI_STATE_ENABLED && wifiAP.getWifiApState() != wifiAP.WIFI_STATE_ENABLING) {
            wifiAP.toggleWifiAP(wifi, MainActivity.this);
        }
    }
    updateStatusDisplay();
}

@Override
public void onPause() {
    super.onPause();
    boolean wifiApIsOn = wifiAP.getWifiApState()==wifiAP.WIFI_STATE_ENABLED || wifiAP.getWifiApState()==wifiAP.WIFI_STATE_ENABLING;
    if (wifiApIsOn){
        wasApEnabled = true;
        wifiAP.toggleWifiAP(wifi, MainActivity.this);
    }else {
        wasApEnabled = false;
    }
    updateStatusDisplay();
}

public static void updateStatusDisplay(){
    if (wifiAP.getWifiApState()==wifiAP.WIFI_STATE_ENABLED || wifiAP.getWifiApState()==wifiAP.WIFI_STATE_ENABLING){
        apButton.setText("Turn Off");
    }else {
        apButton.setText("Turn on");
    }
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0,0,0, "Get Clients");
    return super.onCreateOptionsMenu(menu);
}

public boolean onOptionsItemSelected(int featureId, MenuItem item) {
    switch (item.getItemId()){
        case 0:
            scan();
            break;
    }
    return super.onMenuItemSelected(featureId, item);
}
}

AccessPoint class

public class AccessPoint extends Activity {
private static int constant = 0;
private Context context;

private static int WIFI_STATE_UNKNOWN = -1;
private static int WIFI_STATE_DISABLING = 0;
private static int WIFI_STATE_DISABLED = 1;
public static int WIFI_STATE_ENABLING = 2;
public static int WIFI_STATE_ENABLED = 3;
private static int WIFI_STATE_FAILED = 4;

final static String[] WIFI_STATE_TEXTSTATE = new String[]{
        "DISABLING","DISABLED","ENABLING","ENABLED","FAILED"
};

private WifiManager wifi;
private String TAG = "WifiAP";

private int stateWifi = -1;
private boolean alwaysEnabledWifi = true;


//enable or disable the wifi
public void toggleWifiAP(WifiManager wifiHandler, Context context){
    if (wifi == null){
        wifi = wifiHandler;
    }
    boolean wifiApIsOn = getWifiApState() == WIFI_STATE_ENABLED || getWifiApState()==WIFI_STATE_ENABLING;
    new SetWifiApTask(!wifiApIsOn, false, context).execute();
}

private int setWifiApEnabled(boolean enabled){
    Log.d(TAG, "Set wifi enabled called" + enabled);

    WifiConfiguration config = new WifiConfiguration();
    config.SSID = "Attend Lecture";
    config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);

    //remember wireless state
    if (enabled && stateWifi == -1){
        stateWifi = wifi.getWifiState();
    }

    //disable the wireless
    if (enabled && wifi.getConnectionInfo() !=null){
        Log.d(TAG, "disable wifi: calling");
        wifi.setWifiEnabled(false);
        int loopMax = 10;
        while (loopMax > 0 && wifi.getWifiState() != WifiManager.WIFI_STATE_DISABLED){
            Log.d(TAG, "Disable Wifi: Waiting, pass:" + (10-loopMax));
            try{
                Thread.sleep(500);
                loopMax--;
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        Log.d(TAG, "Disabling wifi is done, pass: " + (10-loopMax));
    }

    //enable and disable wifi AP
    int state = WIFI_STATE_UNKNOWN;
    try {
        Log.d(TAG, (enabled?"enabling":"Disabling")+"wifi ap: calling");
        wifi.setWifiEnabled(false);
        Method method1 = wifi.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
        method1.invoke(wifi, config, enabled);
        Method method2 = wifi.getClass().getMethod("getWifiState");
        state = (Integer)method2.invoke(wifi);
    }catch (Exception e){
        //Log.e(WIFI_SERVICE, e.getMessage());
    }

    //Use thread while processing occurs
    if (!enabled){
        int loopMax = 10;
        while (loopMax>0 && (getWifiApState()==WIFI_STATE_DISABLING || getWifiApState()==WIFI_STATE_ENABLED || getWifiApState()==WIFI_STATE_FAILED)){
            Log.d(TAG, (enabled?"enabling": "disabling")+ "wifi AP: waiting, pass:" + (10-loopMax));
            try {
                Thread.sleep(500);
                loopMax--;
            }catch (Exception e){

            }
        }
        Log.d(TAG, (enabled?"enabling":"disabling")+" Wifi ap: done, pass: " + (10-loopMax));

        //enable the wifi
        if (stateWifi==WifiManager.WIFI_STATE_ENABLED || stateWifi==WifiManager.WIFI_STATE_ENABLING || stateWifi==WifiManager.WIFI_STATE_UNKNOWN || alwaysEnabledWifi){
            Log.d(TAG, "enable wifi: Calling");
            wifi.setWifiEnabled(true);
            //this way it doesnt hold things up and waits for it to get enabled
        }

        stateWifi = -1;
    }else if (enabled){
        int loopMax = 10;
        while (loopMax>0 && (getWifiApState()==WIFI_STATE_ENABLING || getWifiApState()==WIFI_STATE_DISABLED || getWifiApState()==WIFI_STATE_FAILED)){
            Log.d(TAG, (enabled?"Enabling": "disabling") + "wifi ap: waiting, pass: " + (10-loopMax));
            try{
                Thread.sleep(500);
                loopMax--;
            }catch (Exception e){

            }
        }
        Log.d(TAG, (enabled?"Enabling": "disabling")+ "wifi ap: done, pass: " + (10-loopMax));
    }
    return state;
}

//Get the wifi AP state
public int getWifiApState(){
    int state = WIFI_STATE_UNKNOWN;
    try {
        Method method2 = wifi.getClass().getMethod("getWifiApState");
        state = (Integer) method2.invoke(wifi);
    }catch (Exception e){

    }

    if (state>=10){
        constant=10;
    }

    WIFI_STATE_DISABLING = 0+constant;
    WIFI_STATE_DISABLED = 1+constant;
    WIFI_STATE_ENABLING = 2+constant;
    WIFI_STATE_ENABLED = 3+constant;
    WIFI_STATE_FAILED = 4+constant;

    Log.d(TAG, "getWifiApState " + (state==-1?"UNKNOWN":WIFI_STATE_TEXTSTATE[state-constant]));
    return state;
}

class SetWifiApTask extends AsyncTask<Void, Void, Void>{
    boolean mMode;
    boolean mFinish;
    ProgressDialog pDialog;

    public SetWifiApTask(boolean mode, boolean finish, Context context){
        mMode = mode;
        mFinish = finish;
        pDialog = new ProgressDialog(context);
    }

    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        pDialog.setTitle("Turning on Access Point " + (mMode?"On":"Off" + "..."));
        pDialog.setMessage("Please wait a moment...");
        pDialog.show();
    }

    @Override
    protected void onPostExecute(Void aVoid){
        super.onPostExecute(aVoid);
        try {
            pDialog.dismiss();
            MainActivity.updateStatusDisplay();
        }catch (IllegalArgumentException e){

        };
        if (mFinish){
            finish();
        }
    }

    @Override
    protected Void doInBackground(Void... params) {
        setWifiApEnabled(mMode);
        return null;
    }
}

//get the list connected to the wifi hotspot
public void getClientList(boolean onlyReachable, FinishScanListener finishListener){
    getClientList(onlyReachable, 300, finishListener);
}

public void getClientList(final boolean onlyReachable, final int reachableTimeout, final FinishScanListener finishListener){

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            BufferedReader br = null;
            final ArrayList<ClientScanResult> result = new ArrayList<>();

            try {
                br = new BufferedReader(new FileReader("/proc/net/arp"));
                String line;
                while ((line = br.readLine()) != null){
                    String[] splitted = line.split(" +");

                    if ((splitted !=null) && (splitted.length >=4)){
                        String mac = splitted[3];

                        if (mac.matches("..:..:..:..:..:..")){
                            boolean isReachable = InetAddress.getByName(splitted[0]).isReachable(reachableTimeout);

                            if (!onlyReachable || isReachable){
                                result.add(new ClientScanResult(splitted[0], splitted[3], splitted[5], isReachable));
                            }
                        }
                    }
                }
            }catch (Exception e){
                Log.e(this.getClass().toString(), e.toString());
            }finally {
                try {
                    br.close();
                }catch (IOException e){
                    Log.e(this.getClass().toString(), e.getMessage());
                }
            }
            //Get handler that will be used to post to main thread
            Handler mainHandler = new Handler(context.getMainLooper());
            Runnable myRunnable = new Runnable() {
                @Override
                public void run() {
                    finishListener.onFinishScan(result);
                }
            };
            mainHandler.post(myRunnable);
        }
    };

    Thread myThread = new Thread(runnable);
    myThread.start();

}


}
like image 209
hero8110 Avatar asked Mar 08 '15 16:03

hero8110


1 Answers

As far as I can see it, your Context is null. You have a private variable Context at the top of your class, but you never assign anything to it, so it's null.

Give your AccessPoint a constructor and put something like

context = getContext();

inside of it.

like image 164
Playground Avatar answered Oct 04 '22 22:10

Playground