2013-06-26 58 views
0

我已經寫了一個Android應用程序,這是檢查互聯網連接到谷歌(通過套接字,我猜)!當我從我的Networking-Thread(檢查方法)得到答案時,我嘗試用setText更新我的textview,但是我得到了一個nullpointerexception!這可能是我的代碼中一個安靜的簡單錯誤,但我根本不明白。Android - 線程之間的問題(NullPointerException)

這裏是我的MainActivity:

package at.springsits.michi.hostchecker; 

import android.os.Bundle; 
import android.app.Activity; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class Main extends Activity implements OnClickListener { 

    TextView connection_internet; 
    Button pingbutton; 
    Networking net; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     connection_internet = (TextView) findViewById(R.id.connection); 
     pingbutton = (Button) findViewById(R.id.ping_button); 
     pingbutton.setOnClickListener(this); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public void onClick(View v) { 
     new Thread(new Runnable() { 
      public void run() { 
      `enter code here`net.connectionToGoogle = Networking.testPingToGoogle(); 
       runOnUiThread(new Runnable() { 
        public void run() { 
         connection_internet.setText(String 
           .valueOf(net.connectionToGoogle)); 
        } 
       }); 
      } 
     }).start(); 

    } 

} 

的logcat:

06-26 16:38:00.030: D/libEGL(2756): loaded /system/lib/egl/libEGL_adreno200.so 
06-26 16:38:00.030: D/libEGL(2756): loaded /system/lib/egl/libGLESv1_CM_adreno200.so 
06-26 16:38:00.030: D/libEGL(2756): loaded /system/lib/egl/libGLESv2_adreno200.so 
06-26 16:38:00.030: I/Adreno200-EGL(2756): <eglInitialize:269>: EGL 1.4 QUALCOMM build: Nondeterministic AU_full_mako_PARTNER-ANDROID/JB-MR1-DEV_CL2961380_release_AU (CL2961380) 
06-26 16:38:00.030: I/Adreno200-EGL(2756): Build Date: 12/10/12 Mon 
06-26 16:38:00.030: I/Adreno200-EGL(2756): Local Branch: 
06-26 16:38:00.030: I/Adreno200-EGL(2756): Remote Branch: m/partner-android/jb-mr1-dev 
06-26 16:38:00.030: I/Adreno200-EGL(2756): Local Patches: NONE 
06-26 16:38:00.030: I/Adreno200-EGL(2756): Reconstruct Branch: NOTHING 
06-26 16:38:00.070: D/OpenGLRenderer(2756): Enabling debug mode 0 
06-26 16:38:03.414: W/dalvikvm(2756): threadid=11: thread exiting with uncaught exception (group=0x40bf9930) 
06-26 16:38:03.414: E/AndroidRuntime(2756): FATAL EXCEPTION: Thread-6549 
06-26 16:38:03.414: E/AndroidRuntime(2756): java.lang.NullPointerException 
06-26 16:38:03.414: E/AndroidRuntime(2756):  at at.springsits.michi.hostchecker.Main$1.run(Main.java:37) 
06-26 16:38:03.414: E/AndroidRuntime(2756):  at java.lang.Thread.run(Thread.java:856) 
+0

郵政logcat的,所以我們知道什麼是'null' – codeMagic

+1

'net'是空的,我想。 'net net'然後'net.connectionToGoogle = Networking.testPingToGoogle()'。 –

+0

我想我需要一個網絡網=新網絡()的地方? – MIC

回答

0

net爲空。在實例化之前,您正試圖訪問其中的一個字段。

聲明它在這裏:

Networking net 

然後嘗試並獲得它在onClick()

net.connectionToGoogle = Networking.testPingToGoogle() 

但現階段netnull。實例在你onCreate()或類似嘗試使用它之前:

net = new Networking();