0

I'm trying to scan Access Points and list them. When I'm trying to run it in emulator, there is no error but app stop working message come to screen. I tried generate APK and start on real phone app cannot install to the phone.

This my code:

package com.ahmet.apscan;
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);

            }


        }
    };

}

And this is my Xml:

<?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" />

This is my logcat:

10-25 14:56:19.808 2010-4163/? I/GCoreUlr: Starting service, intent=Intent {                             act=com.google.android.location.reporting.ACTION_UPDATE_WORLD cmp=com.google.android.gms/com.google.android.location.reporting.service.DispatchingService (has extras) }, extras=Bundle[{receiverAction=com.google.android.location.reporting.PHENOTYPE_FLAGS_CHANGED}]
10-25 14:56:19.812 2010-4137/? I/GCoreUlr: WorldUpdater received intent Intent { act=com.google.android.location.reporting.ACTION_UPDATE_WORLD cmp=com.google.android.gms/com.google.android.location.reporting.service.DispatchingService (has extras) } with receiverAction com.google.android.location.reporting.PHENOTYPE_FLAGS_CHANGED
10-25 14:56:19.813 2010-4137/? E/GCoreUlr: Bad errorId configuring silent feedback
10-25 14:56:19.814 2010-4137/? I/GCoreUlr: WorldUpdater:com.google.android.location.reporting.PHENOTYPE_FLAGS_CHANGED: Ensuring that reporting is stopped because of reasons: (no Google accounts)
10-25 14:56:19.817 2010-4137/? I/GCoreUlr: Unbound from all location providers
10-25 14:56:19.817 2010-4137/? I/GCoreUlr: Place inference reporting - stop
10-25 14:56:19.819 2010-2010/? I/GCoreUlr: DispatchingService.onDestroy()
10-25 14:56:19.819 2010-2010/? I/GCoreUlr: Stopping handler for UlrDispSvcFast
10-25 14:56:19.822 2010-2010/? I/GCoreUlr: Unbound from all location providers
10-25 14:56:19.822 2010-2010/? I/GCoreUlr: Place inference reporting - stop

How can I fix this Bad errorId configuring silent feedback?

Now I tried Clean Project & Make Project. After that I build new Emulator and logcat changed like this;

10-25 15:21:22.302 4752-4752/? I/art: Not late-enabling -Xcheck:jni (already on)
10-25 15:21:22.302 4752-4752/? W/art: Unexpected CPU variant for X86 using defaults: x86
10-25 15:21:22.317 4752-4759/? E/art: Failed sending reply to debugger: Broken pipe
10-25 15:21:22.317 4752-4759/? I/art: Debugger is no longer active
10-25 15:21:22.317 4752-4759/? I/art: Starting a blocking GC Instrumentation
10-25 15:21:22.391 4752-4752/? W/System: ClassLoader referenced unknown path: /data/app/com.ahmet.apscan-2/lib/x86
10-25 15:21:22.396 4752-4752/? I/InstantRun: starting instant run server: is main process
10-25 15:21:22.414 4752-4752/? D/AndroidRuntime: Shutting down VM
10-25 15:21:22.414 4752-4752/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.ahmet.apscan, PID: 4752
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ahmet.apscan/com.ahmet.apscan.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:2646)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:154)
                                                     at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
                                                  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.ahmet.apscan.MainActivity.onCreate(MainActivity.java:41)
                                                     at android.app.Activity.performCreate(Activity.java:6662)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                     at android.os.Looper.loop(Looper.java:154) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
Sushin Pv
  • 1,826
  • 3
  • 22
  • 36
Wolde
  • 25
  • 1
  • 8

4 Answers4

0

Use getSupportActionBar() instead of getActionBar(). Also, specify it below setContentView() line.

Sneh Pandya
  • 8,197
  • 7
  • 35
  • 50
0

all is about your getActionBar(), It seems relative to this Check everything in this link.

Djory Krache
  • 347
  • 4
  • 9
0

depending on the update of the post use

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

and put it under setContentView method so yor code should be like this

setContentView(R.layout.activity_main);
getSupportActionBar().setTitle("Widhwan Setup Wizard"); 
Basil Battikhi
  • 2,638
  • 1
  • 18
  • 34
0

I think you are running the app on android version lolipop or above.So

use com.android.support:appcompat-v7 library

Change

public class MainActivity extends Activity implements View.OnClickListener

to

public class MainActivity extends AppCompatActivity implements View.OnClickListener

and change

 getActionBar().setTitle("Widhwan Setup Wizard");
 setContentView(R.layout.activity_main);

to

setContentView(R.layout.activity_main);
getSupportActionBar().setTitle("Widhwan Setup Wizard");
Soham
  • 4,397
  • 11
  • 43
  • 71
  • It worked and i installed to my physical phone by changing APK signature v2 to v1. App working on phone no wifi list shown. I tried this methods on a working project. But I copied everything on that working project to new project because of gradle and want to see Design Tab. It has also worked on my phone but again no wifi list shown on the screen. Before i copied everything it shows but this time no Design tab for project – Wolde Oct 25 '17 at 14:04