奇怪的Android NullPointerException

时间:2017-07-07 03:42:44

标签: java android nullpointerexception

我有一个错误,我已经在堆栈上留下了大约一小时的痕迹,我无法弄清楚我做错了什么。

这最终将控制机器人并包含更多功能,但我现在正在做的是确保手机连接到机器人的wifi网络。我是通过测试页面(192.168.240.1/handshake)并查看它返回的内容来做到这一点的。目前,当我连接到机器人时,它可以正常工作,但如果我没有连接它,它会崩溃。我试图评论我的很多代码,但问题仍然存在。

 package com.ruralsurvivor.robotcontrolapp;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.concurrent.ExecutionException;

public class MainActivity extends AppCompatActivity {
    TextView t;
    TextView t2;
    Button b;
    String z;
    String lResponse;
    private Handler handler = new Handler();
    public Runnable runnable = new Runnable() {
        @Override
        public void run() {
            update();
            handler.postDelayed(runnable, 50);
        }
        public void update() {
            int numberOfLevels = 100;
            android.net.wifi.WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels);
            ProgressBar firstBar = (ProgressBar) findViewById(R.id.determinateBar);
            firstBar.setProgress(level);
            wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
            WifiInfo info = wifiManager.getConnectionInfo();
            String ssid = info.getSSID();
            t.setText("Network: " + ssid);
            z = "http://192.168.240.1/handshake";
            try {
                lResponse = new HTTPRequest().execute(z).get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (NullPointerException e) {
                e.printStackTrace();

                lResponse = "error";
            }
            if (!lResponse.equals("Schmuck me sideways")) {
                b.setEnabled(false);
                b.setAlpha(0.75f);
                t2.setTextColor(Color.parseColor("#c10508"));
                t2.setText("Not Connected");
            } else {
                b.setEnabled(true);
                b.setAlpha(1f);
                t2.setTextColor(Color.parseColor("#1eb500"));
                t2.setText("Connected");
            }

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t = new TextView(this);
        t=(TextView)findViewById(R.id.textView2);
        t2 = new TextView(this);
        t2 =(TextView)findViewById(R.id.textView3);
        b=(Button)findViewById(R.id.button2);
        start();
    }

    public void start() {
        runnable.run();
    }
    public void onClick(View view) {
        Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
        startActivity(intent);
    }

    public void clickMe(View v) {
        handler.removeCallbacks(runnable);
        setContentView(R.layout.control);
        WebView myWebView = (WebView) findViewById(R.id.webView);
        myWebView.loadUrl("http://192.168.240.1:8080/?action=stream");
    }
}

这是我的堆栈跟踪:

07-06 23:21:15.713 18517-18517/com.ruralsurvivor.robotcontrolapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                   Process: com.ruralsurvivor.robotcontrolapp, PID: 18517
                                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ruralsurvivor.robotcontrolapp/com.ruralsurvivor.robotcontrolapp.MainActivity}: java.lang.NullPointerException
                                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
                                                                                       at android.app.ActivityThread.access$800(ActivityThread.java:139)
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                       at android.os.Looper.loop(Looper.java:136)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5097)
                                                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                       at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                                       at dalvik.system.NativeStart.main(Native Method)
                                                                                    Caused by: java.lang.NullPointerException
                                                                                       at com.ruralsurvivor.robotcontrolapp.MainActivity$1.update(MainActivity.java:57)
                                                                                       at com.ruralsurvivor.robotcontrolapp.MainActivity$1.run(MainActivity.java:34)
                                                                                       at com.ruralsurvivor.robotcontrolapp.MainActivity.start(MainActivity.java:83)
                                                                                       at com.ruralsurvivor.robotcontrolapp.MainActivity.onCreate(MainActivity.java:79)
                                                                                       at android.app.Activity.performCreate(Activity.java:5248)
                                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
                                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257) 
                                                                                       at android.app.ActivityThread.access$800(ActivityThread.java:139) 
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                       at android.os.Looper.loop(Looper.java:136) 
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5097) 
                                                                                       at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                       at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                                                                                       at dalvik.system.NativeStart.main(Native Method)

这是我用来测试wifi的代码:

package com.ruralsurvivor.robotcontrolapp;

import android.os.AsyncTask;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HTTPRequest extends AsyncTask< String, String, String >
{

    protected String theHTTPString = null;

    @Override
    protected String doInBackground( String... uri )
    {
        URL url = null;
        try
        {
            url = new URL( uri[0] );
        }
        catch( MalformedURLException e )
        {
            e.printStackTrace();
        }

        String responseString = null;
        try
        {
            HttpURLConnection httpclient = (HttpURLConnection) url.openConnection();

            int lResponseCode = httpclient.getResponseCode();
            if( lResponseCode != HttpURLConnection.HTTP_OK )
            {
                httpclient.disconnect();
                throw new IOException( "RESPONSE REJECTED WITH ERROR CODE: " + lResponseCode );
            }
            BufferedReader in = new BufferedReader(new InputStreamReader( httpclient.getInputStream() ));
            StringBuffer lStringBuffer = new StringBuffer();
            String inputLine;
            while( (inputLine = in.readLine()) != null )
            {
                lStringBuffer.append( inputLine );
            }
            httpclient.disconnect();
            in.close();
            responseString = lStringBuffer.toString();
        }
        catch( IOException e )
        {
            e.printStackTrace();
            return responseString;
        }

        theHTTPString = responseString;

        return responseString;
    }
}

0 个答案:

没有答案