Android - Wifi扫描程序崩溃

时间:2017-08-07 14:11:05

标签: java android android-wifi

附在帖子上是我的Android代码,用于简单的wifi扫描应用程序。这是基于API 25/26构建的。每当我启动应用程序时它都会崩溃。我试图在模拟器(Lollipop)上运行,Google Pixel在7.1.2上运行。请查看代码并尽可能帮助我。

MainActivity.java



package com.example.fahad.test;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements View.OnClickListener{


    WifiManager wifi;
    ListView lv;
    Button buttonScan;
    int size = 0;
    List<ScanResult> results;

    String ITEM_KEY = "key";
    ArrayList<String> arraylist = new ArrayList<>();
    ArrayAdapter adapter;

    /* Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        getActionBar().setTitle("Widhwan Setup Wizard");

        setContentView(R.layout.activity_main);

        buttonScan = (Button) findViewById(R.id.scan);
        buttonScan.setOnClickListener(this);
        lv = (ListView)findViewById(R.id.wifilist);


        wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (wifi.isWifiEnabled() == false)
        {
            Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", Toast.LENGTH_LONG).show();
            wifi.setWifiEnabled(true);
        }
        this.adapter =  new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,arraylist);
        lv.setAdapter(this.adapter);

        scanWifiNetworks();
    }

    public void onClick(View view)
    {
        scanWifiNetworks();
    }

    private void scanWifiNetworks(){

        arraylist.clear();
        registerReceiver(wifi_receiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

        wifi.startScan();

        Log.d("WifScanner", "scanWifiNetworks");

        Toast.makeText(this, "Scanning....", Toast.LENGTH_SHORT).show();

    }

    BroadcastReceiver wifi_receiver= new BroadcastReceiver()
    {

        @Override
        public void onReceive(Context c, Intent intent)
        {
            Log.d("WifScanner", "onReceive");
            results = wifi.getScanResults();
            size = results.size();
            unregisterReceiver(this);

            try
            {
                while (size >= 0)
                {
                    size--;
                    arraylist.add(results.get(size).SSID);
                    adapter.notifyDataSetChanged();
                }
            }
            catch (Exception e)
            {
                Log.w("WifScanner", "Exception: "+e);

            }


        }
    };

}
&#13;
&#13;
&#13;

activity_main.xml中

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/wifilist"
        android:layout_width="match_parent"
        android:layout_height="312dp"
        android:layout_weight="0.97" />


    <Button
        android:id="@+id/scan"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="bottom"
        android:layout_margin="15dp"
        android:background="@android:color/holo_green_light"
        android:text="Scan Again" />
</LinearLayout>
&#13;
&#13;
&#13;

的AndroidManifest.xml

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.fahad.test">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
&#13;
&#13;
&#13;

&#13;
&#13;
08-08 00:18:04.891 2887-2887/com.example.fahad.test E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.example.fahad.test, PID: 2887
                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fahad.test/com.example.fahad.test.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                                                                          at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                          at android.os.Looper.loop(Looper.java:135)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                          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:899)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
                                                                       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
                                                                          at com.example.fahad.test.MainActivity.onCreate(MainActivity.java:40)
                                                                          at android.app.Activity.performCreate(Activity.java:5937)
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
                                                                          at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                          at android.os.Looper.loop(Looper.java:135) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5221) 
                                                                          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:899) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
08-08 00:18:04.895 1517-1846/system_process W/ActivityManager:   Force finishing activity com.example.fahad.test/.MainActivity
08-08 00:18:04.910 1138-1516/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 1019904
                                                 
                                                 
                                                 [ 08-08 00:18:04.914  1517: 1846 D/         ]
                                                 HostConnection::get() New Host Connection established 0x9c55ad80, tid 1846
08-08 00:18:04.919 1138-1138/? E/EGL_emulation: tid 1138: eglCreateSyncKHR(1865): error 0x3004 (EGL_BAD_ATTRIBUTE)
08-08 00:18:04.974 1517-2907/system_process D/OpenGLRenderer: Render dirty regions requested: true
08-08 00:18:04.974 1517-1538/system_process D/Atlas: Validating map...
                                                     
                                                     [ 08-08 00:18:05.033  1517: 2907 D/         ]
                                                     HostConnection::get() New Host Connection established 0x9d9fd470, tid 2907
08-08 00:18:05.040 1517-2907/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
08-08 00:18:05.040 1517-2907/system_process W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
08-08 00:18:05.053 1517-2907/system_process D/EGL_emulation: eglCreateContext: 0xac7a6520: maj 3 min 1 rcv 4
08-08 00:18:05.060 1517-2907/system_process D/EGL_emulation: eglMakeCurrent: 0xac7a6520: ver 3 1
08-08 00:18:05.061 1517-2907/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
08-08 00:18:05.061 1517-2907/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
08-08 00:18:05.061 1517-2907/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
08-08 00:18:05.066 1517-2907/system_process D/OpenGLRenderer: Enabling debug mode 0
08-08 00:18:05.086 1517-2907/system_process D/EGL_emulation: eglMakeCurrent: 0xac7a6520: ver 3 1
08-08 00:18:05.414 2347-2665/com.android.calendar I/GlobalDismissManager: no sender configured
08-08 00:18:05.414 2347-2665/com.android.calendar D/AlertService: Beginning updateAlertNotification
08-08 00:18:05.426 2347-2665/com.android.calendar D/AlertService: No fired or scheduled alerts
08-08 00:18:05.437 1517-1538/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{1923856f u0 com.example.fahad.test/.MainActivity t28 f}
08-08 00:18:05.439 2347-2665/com.android.calendar D/AlertService: Scheduling next alarm with AlarmScheduler. sEventReminderReceived: null
08-08 00:18:05.453 2347-2665/com.android.calendar D/AlarmScheduler: No events found starting within 1 week.
08-08 00:18:05.476 1821-1999/com.android.launcher D/EGL_emulation: eglMakeCurrent: 0xaef0d100: ver 3 1
08-08 00:18:05.568 1602-2022/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xaef21100: ver 3 1
08-08 00:18:05.581 1602-2022/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xaef21100: ver 3 1
08-08 00:18:05.585 1602-2022/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xaef21100: ver 3 1
08-08 00:18:05.625 1602-2022/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xaef21100: ver 3 1
08-08 00:18:05.629 1602-2022/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xaef21100: ver 3 1
08-08 00:18:05.642 1602-2022/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xaef21100: ver 3 1
08-08 00:18:05.646 1602-2022/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xaef21100: ver 3 1
08-08 00:18:05.678 1602-2022/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xaef21100: ver 3 1
08-08 00:18:05.692 1602-2022/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xaef21100: ver 3 1
08-08 00:18:05.697 1602-2022/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xaef21100: ver 3 1
08-08 00:18:05.717 1602-2022/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xaef21100: ver 3 1
08-08 00:18:05.729 1602-2022/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xaef21100: ver 3 1
08-08 00:18:05.735 2100-2211/com.google.android.gms I/Icing: Indexing 683F7A6F22BBD90B1D3ABECD88AADD143975EBF3 from com.google.android.gms
08-08 00:18:05.736 1602-2022/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xaef21100: ver 3 1
08-08 00:18:05.747 1602-2022/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xaef21100: ver 3 1
08-08 00:18:05.752 1602-2022/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xaef21100: ver 3 1
08-08 00:18:05.763 1602-2022/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xaef21100: ver 3 1
08-08 00:18:05.770 1602-2022/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xaef21100: ver 3 1
08-08 00:18:05.780 1602-2022/com.android.systemui D/EGL_emulation: eglMakeCurrent: 0xaef21100: ver 3 1
08-08 00:18:05.817 2100-2211/com.google.android.gms I/Icing: Indexing done 683F7A6F22BBD90B1D3ABECD88AADD143975EBF3
08-08 00:18:06.050 1821-1999/com.android.launcher W/OpenGLRenderer: Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer...
08-08 00:18:06.050 1821-1999/com.android.launcher W/OpenGLRenderer: Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer...
08-08 00:18:06.050 1821-1999/com.android.launcher W/OpenGLRenderer: Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer...
08-08 00:18:08.007 1517-1532/system_process I/ActivityManager: Killing 1877:com.android.externalstorage/u0a6 (adj 15): empty #17
08-08 00:18:08.008 1517-1532/system_process W/libprocessgroup: failed to open /acct/uid_10006/pid_1877/cgroup.procs: No such file or directory
08-08 00:18:08.013 1517-1625/system_process W/libprocessgroup: failed to open /acct/uid_10006/pid_1877/cgroup.procs: No such file or directory
&#13;
&#13;
&#13;

真的需要你的帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

在您的MainActivity中,onCreate()

getSupportActionBar().setTitle("Widhwan Setup Wizard");

并扩展AppCompatActivity而不是Activity