wifi 신호 arraylist로 받아서 출력하는데 계속 NullPointerException 이 발생합니다..

조회수 118회

package com.example.wifimapsuns;

import android.Manifest; import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.wifi.ScanResult; import android.net.wifi.WifiManager; import android.os.Build; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity {

WifiManager myWifiManager;
boolean wasEnabled;
ArrayList<FingerPrintBean> mFingerPrintBeanArrayList;
ArrayList<FingerPrintBean> pFingerPrintBeanArrayList;
ArrayAdapter<FingerPrintBean> mArrayAdapter;
//Intent mIntent;

//dbInstanceBean mdbInstaceBean;


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

    myWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    mFingerPrintBeanArrayList = new ArrayList<FingerPrintBean>();

    ListView mListView = (ListView) findViewById(R.id.lv1);
    Button mButton = (Button) findViewById(R.id.button);
    final ImageView imageView=(ImageView)findViewById(R.id.imageView);
   // imageView.setVisibility(imageView.INVISIBLE);
    final TextView textView = (TextView) findViewById(R.id.ssidid);

    mArrayAdapter = new ArrayAdapter<FingerPrintBean>(this, android.R.layout.simple_list_item_1, mFingerPrintBeanArrayList);
    mListView.setAdapter(mArrayAdapter);

    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View pView) {
            //mArrayAdapter.clear();

            refresh();

            String macc = null;
            try{
            try {
                String ovj = mFingerPrintBeanArrayList.get(0).toString();
                macc = ovj.substring(27, 46);
                textView.setText(macc);
            } catch (NullPointerException ignored) {
                Log.d("dddddddd","ㅡㅡㅡㅡㅜㅜㅜㅜㅜㅜㅜㅜㅜㅜㅜㅜㅜㅜㅜㅜㅜㅜㅜㅜㅜㅜ");
            }}
            catch (IndexOutOfBoundsException e){
                Log.d("index뺌!!!!","333333333333333333333333333333333333333333333333");
            }
            //imageView.setVisibility(imageView.VISIBLE);

            //////////다섯번 정도 누르면 에러나서 꺼짐//////////////

            switch (macc) {
                case "'00:07:89:25:bd:62'":
                    imageView.setImageResource(R.mipmap.ic_area1);
                    break;
                case "'64:e5:99:ca:86:32'":
                    imageView.setImageResource(R.mipmap.ic_area2);
                    break;
                default:
                    imageView.setImageResource(R.mipmap.ic_dog);
                    break;
            }
        }
    });




}


public void refresh(){
    //mFingerPrintBeanArrayList = new ArrayList<FingerPrintBean>();
   mFingerPrintBeanArrayList.clear();

// pFingerPrintBeanArrayList.clear();

    wasEnabled = myWifiManager.isWifiEnabled();

    if (!wasEnabled)
        myWifiManager.setWifiEnabled(true);//와이파이 사용 가능 여부

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Log.d("pras","inside permission");
        requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 0x12345);
    }

    FingerPrintBean mFingerPrintBean;



    if (myWifiManager.isWifiEnabled()) {
        if (myWifiManager.startScan()) {
            List<ScanResult> scans = myWifiManager.getScanResults();

            //int topThree = 3;
            if (scans != null && !scans.isEmpty()) {
                int i=0;
                for (ScanResult scan : scans) {
                    Double level = (double)scan.level;
                    mFingerPrintBean = new FingerPrintBean(scan.BSSID, scan.SSID, scan.level);
                    mFingerPrintBeanArrayList.add(mFingerPrintBean);
                }
            } else {
                Log.d("pras", "inside else");
            }
        }
    }

//////////////////////////////////////////////////////try-catch 사용하기//////////////

    if (myWifiManager.isWifiEnabled()) {
        if (myWifiManager.startScan()) {
            int i=0;
            pFingerPrintBeanArrayList = new ArrayList<FingerPrintBean>();
            try{
            for(i=0; i<10; i++){
                List<ScanResult> scans = myWifiManager.getScanResults();

                if (scans != null && !scans.isEmpty()) {
                    for (ScanResult scan : scans) {
                        Double level = (double)scan.level;
                        mFingerPrintBean = new FingerPrintBean(scan.BSSID, scan.SSID, scan.level);
                        pFingerPrintBeanArrayList.add(mFingerPrintBean);
                    }
                }
            }
            }
                catch(IndexOutOfBoundsException e){
                    Log.d("pras", "inside  else3333333333333333");
                }
            }
        }



    int i=0, k=0;
    try {
        for (i = 0; i < pFingerPrintBeanArrayList.size(); i++) {
            FingerPrintBean someBean = pFingerPrintBeanArrayList.get(i);
            int count = 0;
            for (k = 0; k < pFingerPrintBeanArrayList.size(); k++) {
                if (pFingerPrintBeanArrayList.get(k).equals(someBean)) {
                    count++;
                    int newlevel = someBean.getLevel() + pFingerPrintBeanArrayList.get(k).getLevel();
                    someBean.setLevel(newlevel);
                    pFingerPrintBeanArrayList.remove(k);
                    Log.d("sam", "" + pFingerPrintBeanArrayList.size());
                }
            }
            someBean.setLevel(someBean.getLevel() / count);


            if (!mFingerPrintBeanArrayList.contains(someBean)) {
                mFingerPrintBeanArrayList.add(someBean);
            }
        }
    }catch(NullPointerException e){
        Log.d("hello","kkkkkkkkkkkkkkkkkkkkkkkkk" );
    }

    mArrayAdapter.notifyDataSetChanged();

}

}

오류 창 부분입니다...!!

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
    at com.example.wifimapsuns.MainActivity$1.onClick(MainActivity.java:77)
    at android.view.View.performClick(View.java:7352)
    at android.widget.TextView.performClick(TextView.java:14177)
    at android.view.View.performClickInternal(View.java:7318)
    at android.view.View.access$3200(View.java:846)
    at android.view.View$PerformClick.run(View.java:27801)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7045)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)

오류는 계속 main에서 발생하고 있고요 ㅜㅜ버튼을 통하여 onclick 리스너 달았는데 ㅠㅠ 버튼 2~3번 누르면 앱이 강제 종료 됩니다... 왜 앱이 강제 종료 될까요ㅜㅜㅜ ㅜㅜㅜㅜㅜ제발 도와주세요..!!!

답변을 하려면 로그인이 필요합니다.

Hashcode는 개발자들을 위한 무료 QnA 사이트입니다. 계정을 생성하셔야만 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)

ᕕ( ᐛ )ᕗ
로그인이 필요합니다

Hashcode는 개발자들을 위한 무료 QnA사이트 입니다. 계정을 생성하셔야만 글을 작성하실 수 있습니다.