没有错误,但应用程序不工作Android工作室

时间:2017-10-25 12:19:25

标签: java android

我正在尝试扫描接入点并列出它们。当我试图在模拟器中运行它时,没有错误,但应用程序停止工作消息进入屏幕。我尝试生成APK并开始真正的手机应用程序无法安装到手机。

这是我的代码:

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);

            }


        }
    };

}

这是我的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" />

这是我的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

如何修复此错误配置无提示反馈?

现在我尝试了Clean Project&amp;制作项目。之后我建立了新的模拟器和 logcat改变了这样;

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) 

4 个答案:

答案 0 :(得分:0)

使用getSupportActionBar()代替getActionBar()。另外,请在setContentView()行以下指定。

答案 1 :(得分:0)

所有关于你的getActionBar(),它似乎与this相关 检查此链接中的所有内容。

答案 2 :(得分:0)

取决于帖子的更新 使用

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

并将其置于setContentView方法下 所以你的代码应该是这样的

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

答案 3 :(得分:0)

我认为您在Android版lolipop或更高版本上运行该应用程序。所以

使用com.android.support:appcompat-v7

更改

public class MainActivity extends Activity implements View.OnClickListener

public class MainActivity extends AppCompatActivity implements View.OnClickListener

并更改

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

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