为什么我在虚拟方法onclick监听器上得到nullpointexception

时间:2017-03-22 14:57:42

标签: onclicklistener android-menu android-popupwindow

我正在尝试为app实现弹出窗口,但我想我的实现不正确。我得到nullpointexception虚方法null对象。我想我正在调用null对象上的onclickListener。实现弹出窗口的正确方法是什么。窗口响应点击菜单项“约”任何帮助赞赏。 异常错误

Process: com.example.android.droidscanner, PID: 14958
                                                                                  java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                      at com.example.android.droidscanner.MainActivity.showAboutWindow(MainActivity.java:152)
                                                                                      at com.example.android.droidscanner.MainActivity.onOptionsItemSelected(MainActivity.java:131)

MainActivity

package com.example.android.droidscanner;

import android.content.Intent;
import android.graphics.Color;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    Button btnReadNet;
    TextView textResult;
    ListView listViewNode;
    ArrayList<String> networkList;
    ArrayAdapter<String> adapterNet;
    public String ip;
    Toolbar myToolbar;
    private LayoutInflater popUpInflater;
    private PopupWindow popUpWindow;
    private Button btnDismiss;
    private LinearLayout linearMain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //setting the ActionBar for the activity
        myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);

        btnReadNet = (Button) findViewById(R.id.read_network);
        textResult = (TextView) findViewById(R.id.result);
        //referencing listview from activity_main.xml
        listViewNode = (ListView) findViewById(R.id.nodelist);
        networkList = new ArrayList<>();

        //referencing LinearLayout in activity_main.xml for popup window
        linearMain = (LinearLayout) findViewById(R.id.linearLayout_main);

        //inflating the adapter
        adapterNet = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, networkList);
        listViewNode.setAdapter(adapterNet);

        WifiManager wifiMan = (WifiManager) getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiMan.getConnectionInfo();
        int ipAddress = wifiInfo.getIpAddress();
        ip = String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
        textResult.setText(printOutput(wifiInfo));


        //starting network scanner activity on button press
        btnReadNet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent scanNetIntent = new Intent(MainActivity.this, IpScanActivity.class);
                scanNetIntent.putExtra("ip", ip);
                startActivity(scanNetIntent);
            }
        });
    }

    public String printOutput(WifiInfo wifiInfo) {
        return "Local IP: " + ip + "\n" +
                "SSID: " + wifiInfo.getSSID() + "\n" +
                "BSSID: " + wifiInfo.getBSSID() + "\n" +
                "MAC: " + wifiInfo.getMacAddress() + "\n" +
                "Signal: " + wifiInfo.getRssi() + "db" + "\n" +
                "Speed: " + wifiInfo.getLinkSpeed() + "Mbps" + "\n" +
                //
                "Connection: " + wifiInfo.getSupplicantState();
    }


    /*
    * Infating the menu for toolbar
    */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    /*
    * actions for menu options in toolbar
    */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_ping) {
            Intent pingIntent = new Intent(MainActivity.this, PingActivity.class);
            startActivity(pingIntent);
            return true;
        }

        if (id == R.id.action_netstat) {
            Intent netstatIntent = new Intent(MainActivity.this, NetstatActivity.class);
            startActivity(netstatIntent);
            return true;
        }

        if (id == R.id.action_refresh) {
            //reload this activity (refresh)
            Intent thisActivity = new Intent(this, MainActivity.class);
            startActivity(thisActivity);
            return true;
        }

        if (id == R.id.action_about) {
            showAboutWindow();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /*
    * method to display about popup window with app informmation
    * eg: developer name, version, website
    */
    public void showAboutWindow() {
        //layout inflater for about popup window
        popUpInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
        ViewGroup container = (ViewGroup) popUpInflater.inflate(R.layout.popup_win, null);

        popUpWindow = new PopupWindow(container, 500, 500, true);
        popUpWindow.showAtLocation(linearMain, Gravity.NO_GRAVITY, 500, 500);

        //button reference for popup window dismiss button
        btnDismiss = (Button) findViewById(R.id.dismiss);
        btnDismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popUpWindow.dismiss();
            }
        });

    }
}

弹出窗口xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/app_info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="DroidScanner by zentech"
            android:textSize="18dp"/>

        <TextView
            android:id="@+id/url_info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="http://pctechtips.org"
            android:textSize="18dp"/>

        <Button
            android:id="@+id/dismiss"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="18dp"
            android:text="ok" />
    </LinearLayout>

</LinearLayout>

0 个答案:

没有答案