我是 android 编程新手,我正在尝试通过 android 上的 TCP 客户端接收数据包数据。
从树莓派获取流数据包后,数据包将在我的 Masukan() 函数中解析为变量,该函数返回名为filter的字符串值。
我可以在我的 Adhrs Activity 上获取 Masukan() 的返回值,但我无法从从另一个活动访问的 Masukan() 获取返回值。
该错误表示 Nullpointerexception,我将不胜感激任何帮助。这是我的代码:
package com.uavgama;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
public class Adhrsparse extends Activity {
String[] parsing = null;
private final String server = "192.168.43.20";
Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
Keluar();
}
};
//declaration of textview etc
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adahrs);
status = (TextView) findViewById(R.id.textView16);
paketdata = (TextView) findViewById(R.id.textView17);
tvalt = (TextView) findViewById(R.id.textView18);
tvasp = (TextView) findViewById(R.id.textView19);
tvlat = (TextView) findViewById(R.id.textView20);
tvlon = (TextView) findViewById(R.id.textView21);
tvptc = (TextView) findViewById(R.id.textView22);
tvrol = (TextView) findViewById(R.id.textView23);
tvyaw = (TextView) findViewById(R.id.textView24);
Keluar();
}
public String Keluar(){
String masuk = new String();
try {
masuk = Masukan();
status.setText(masuk);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
// Intent intentcam = new Intent(this,CameraView.class);
// intentcam.putExtra("filter", filter);
// startActivity(intentcam);
e.printStackTrace();
}
handler.postDelayed(runnable, 100);
return masuk;
}
public String Masukan() throws UnknownHostException, IOException{
Socket s = new Socket(server,12345);
//BufferedReader inp = null;
BufferedReader inp = new BufferedReader(new InputStreamReader(s.getInputStream()));
//String serverMsg = null;
String serverMsg = inp.readLine();
//filter keutuhan data kemudian diprint di var paketdata
if ( serverMsg.endsWith("#")) {
filter = serverMsg;
}
paketdata.setText(filter);
parsing = filter.split("\\|");
parsing[0] = parsing[0].replace("$", "");
tvalt.setText(parsing[0]);
tvasp.setText(parsing[1]);
tvlat.setText(parsing[2]);
tvlon.setText(parsing[3]);
tvptc.setText(parsing[4]);
tvrol.setText(parsing[5]);
parsing[6] = parsing[6].replace("#", "");
tvyaw.setText(parsing[6]);
s.close();
return filter;
};
}
和我的另一项活动
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
import com.uavgama.Adhrsparse;
public class CameraView extends Activity{
String[] parsingcam = null;
private final String server = "192.168.43.20";
InetAddress
/** Called when the activity is first created. */
Scanner scanner = new Scanner(System.in);
Handler handler = new Handler();
Runnable runnable = new Runnable()
{
public void run() {
adhrspars();
}
};
TextView statuse;
TextView paketdata;
TextView data;
String latlon;
String filter;
String parse;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
runnable.run();
Intent fltr = getIntent();
latlon = fltr.getStringExtra("filter");
data.setText(latlon);
adhrspars();
adhrs();
}
public void adhrspars(){
adhrs();
}
public void adhrs() {
String parsed = new String();
Adhrsparse adhrs = new Adhrsparse();
try {
parsed = adhrs.Masukan() ;
data.setText(parsed);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.postDelayed(runnable, 100);
};
这是我的日志:
07-23 20:00:14.708: E/AndroidRuntime(24895): FATAL EXCEPTION: main
07-23 20:00:14.708: E/AndroidRuntime(24895): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uavgama/com.uavgama.CameraView}: java.lang.NullPointerException
07-23 20:00:14.708: E/AndroidRuntime(24895): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2063)
07-23 20:00:14.708: E/AndroidRuntime(24895): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1904)
07-23 20:00:14.708: E/AndroidRuntime(24895): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
07-23 20:00:14.708: E/AndroidRuntime(24895): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
07-23 20:00:14.708: E/AndroidRuntime(24895): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:753)
07-23 20:00:14.708: E/AndroidRuntime(24895): at android.widget.TabHost.setCurrentTab(TabHost.java:417)
07-23 20:00:14.708: E/AndroidRuntime(24895): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:158)
07-23 20:00:14.708: E/AndroidRuntime(24895): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
07-23 20:00:14.708: E/AndroidRuntime(24895): at android.view.View.performClick(View.java:4103)
07-23 20:00:14.708: E/AndroidRuntime(24895): at android.view.View$PerformClick.run(View.java:17117)
07-23 20:00:14.708: E/AndroidRuntime(24895): at android.os.Handler.handleCallback(Handler.java:615)
07-23 20:00:14.708: E/AndroidRuntime(24895): at android.os.Handler.dispatchMessage(Handler.java:92)
07-23 20:00:14.708: E/AndroidRuntime(24895): at android.os.Looper.loop(Looper.java:137)
07-23 20:00:14.708: E/AndroidRuntime(24895): at android.app.ActivityThread.main(ActivityThread.java:4744)
07-23 20:00:14.708: E/AndroidRuntime(24895): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 20:00:14.708: E/AndroidRuntime(24895): at java.lang.reflect.Method.invoke(Method.java:511)
07-23 20:00:14.708: E/AndroidRuntime(24895): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-23 20:00:14.708: E/AndroidRuntime(24895): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-23 20:00:14.708: E/AndroidRuntime(24895): at dalvik.system.NativeStart.main(Native Method)
07-23 20:00:14.708: E/AndroidRuntime(24895): Caused by: java.lang.NullPointerException
07-23 20:00:14.708: E/AndroidRuntime(24895): at com.uavgama.CameraView.onCreate(CameraView.java:48)
07-23 20:00:14.708: E/AndroidRuntime(24895): at android.app.Activity.performCreate(Activity.java:5008)
07-23 20:00:14.708: E/AndroidRuntime(24895): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-23 20:00:14.708: E/AndroidRuntime(24895): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2027)
07-23 20:00:14.708: E/AndroidRuntime(24895): ... 18 more
谢谢你的帮助