好的,所以根据本教程,我有以下代码:
http://android-er.blogspot.ro/2015/12/lookup-manufacturer-info-by-mac-address.html

但是我做了一些更改!
我不在MainActivity.java中运行它,而是尝试在scanwifi.java中运行它

所以我在scanwifi.java中的代码是:

    package com.nliplace.nli.unealta;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class scanwifi extends AppCompatActivity {

    Button btnRead;
    TextView textResult;

    ListView listViewNode;
    ArrayList<Node> listNote;
    ArrayAdapter<Node> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnRead = (Button)findViewById(R.id.readclient);
        textResult = (TextView)findViewById(R.id.result);

        listViewNode = (ListView)findViewById(R.id.nodelist);
        listNote = new ArrayList<>();
        ArrayAdapter<Node> adapter =
                new ArrayAdapter<Node>(
                        scanwifi.this,
                        android.R.layout.simple_list_item_1,
                        listNote);
        listViewNode.setAdapter(adapter);

        listViewNode.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Node node = (Node) parent.getAdapter().getItem(position);
                Toast.makeText(scanwifi.this,
                        "MAC:\t" + node.mac + "\n" +
                                "IP:\t" + node.ip + "\n" +
                                "company:\t" + node.company + "\n" +
                                "country:\t" + node.country + "\n" +
                                "addressL1:\t" + node.addressL1 + "\n" +
                                "addressL2:\t" + node.addressL2 + "\n" +
                                "addressL3:\t" + node.addressL3 + "\n" +


                "type:\t" + node.type + "\n" +
                            "startHex:\t" + node.startHex + "\n" +
                            "endHex:\t" + node.endHex + "\n" +
                            "startDec:\t" + node.startDec + "\n" +
                            "endDec:\t" + node.endDec,
                    Toast.LENGTH_SHORT).show();
        }
    });

    btnRead.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new TaskReadAddresses(listNote, listViewNode).execute();
        }
    });
}



class Node {
    String ip;
    String mac;

    String jsonBody;
    String startHex;
    String endHex;
    String startDec;
    String endDec;
    String company;
    String addressL1;
    String addressL2;
    String addressL3;
    String country;
    String type;

    String remark;

    String queryString = "http://www.macvendorlookup.com/api/v2/";

    Node(String ip, String mac){
        this.ip = ip;
        this.mac = mac;
        queryMacVendorLookup();
    }

    @Override
    public String toString() {
        return "IP: " + ip + "\n" + "MAC: " + mac + "\n" + company + "\n" + remark;
    }

    private String sendQuery(String qMac) throws IOException{
        String result = "";

        URL searchURL = new URL(queryString + qMac);

        HttpURLConnection httpURLConnection = (HttpURLConnection) searchURL.openConnection();

        if(httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK){
            InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader,
                    8192);

            String line = null;
            while((line = bufferedReader.readLine()) != null){
                result += line;
            }

            bufferedReader.close();
        }

        return result;
    }


    private void ParseResult(String json){

        try {
            JSONArray jsonArray = new JSONArray(json);
            JSONObject jsonObject = (JSONObject) jsonArray.get(0);
            startHex = jsonObject.getString("startHex");
            endHex = jsonObject.getString("endHex");
            startDec = jsonObject.getString("startDec");
            endDec = jsonObject.getString("endDec");
            company = jsonObject.getString("company");
            addressL1 = jsonObject.getString("addressL1");
            addressL2 = jsonObject.getString("addressL2");
            addressL3 = jsonObject.getString("addressL3");
            country = jsonObject.getString("country");
            type = jsonObject.getString("type");
            remark = "OK";

        } catch (JSONException e) {
            e.printStackTrace();
            remark = e.getMessage();
        }

    }

    private void queryMacVendorLookup(){
        try {
            jsonBody = sendQuery(mac);
            ParseResult(jsonBody);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

private class TaskReadAddresses extends AsyncTask<Void, Node, Void> {

    ArrayList<Node> array;
    ListView listView;

    TaskReadAddresses(ArrayList<Node> array, ListView v){
        listView = v;
        this.array = array;
        array.clear();
        textResult.setText("querying...");
    }

    @Override
    protected Void doInBackground(Void... params) {
        readAddresses();

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        textResult.setText("Done");
    }

    @Override
    protected void onProgressUpdate(Node... values) {
        listNote.add(values[0]);
        ((ArrayAdapter)(listView.getAdapter())).notifyDataSetChanged();

    }

    private void readAddresses() {

        BufferedReader bufferedReader = null;

        try {
            bufferedReader = new BufferedReader(new FileReader("/proc/net/arp"));

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                String[] splitted = line.split(" +");
                if (splitted != null && splitted.length >= 4) {
                    String ip = splitted[0];
                    String mac = splitted[3];
                    if (mac.matches("..:..:..:..:..:..")) {
                        Node thisNode = new Node(ip, mac);
                        publishProgress(thisNode);
                    }
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

}

我在activity_scanwifi.xml中的代码是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical"
    tools:context="com.nliplace.nli.unealta.scanwifi">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <Button
        android:id="@+id/readclient"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="Read Ip/MAC addresses"/>

    <TextView
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <ListView
        android:id="@+id/nodelist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

当我用按钮调用scanwifi Activity 时,我进入开发人员控制台,出现此错误:
FATAL EXCEPTION: main
  Process: com.nliplace.nli.unealta, PID: 29149
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nliplace.nli.unealta/com.nliplace.nli.unealta.scanwifi}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2695)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
      at android.app.ActivityThread.access$900(ActivityThread.java:177)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5910)
      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:1405)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
      at com.nliplace.nli.unealta.scanwifi.onCreate(scanwifi.java:49)
      at android.app.Activity.performCreate(Activity.java:6178)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2648)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769) 
      at android.app.ActivityThread.access$900(ActivityThread.java:177) 


      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5910) 
      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:1405) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 

那么,为什么我会得到这个想法以及如何解决呢?

最佳答案

我在想的只是您设置setContentView(R.layout.activity_main);,而应该设置为setContentView(R.layout.activity_scanwifi);的事实。

这会导致NullPointerException,因为findViewById在布局中找不到视图。

我希望这可以帮助你。

08-05 03:25