这是我的代码
package com.example.akash.myapplication;
//Required import files here
public class MainActivity extends Activity implements Runnable
{
public TextView tex;
public Button button;
public EditText edi;
public static Socket client;
String serverName = "192.168.0.100";
int port = 4444;
protected void onCreate(Bundle savedValues) {
button= (Button) findViewById(R.id.button1);
tex= (TextView) findViewById(R.id.textView1);
edi= (EditText) findViewById(R.id.editText1);
Runnable r = new MainActivity();
final Thread t= new Thread(r);
t.start();
// Register the onClick listener with the implementation above
button.setOnClickListener(new OnClickListener() {
public void onClick(View v){
try {
t.wait();
/*System.out.println("Enter your name: ");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
System.out.println("Connecting to " + serverName + " on port " + port);*/
client = new Socket(serverName, port);
//System.out.println("Just connected to " + client.getRemoteSocketAddress());
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeUTF("Hello from " + client.getLocalSocketAddress());
String msg = edi.getText().toString();
out.writeUTF("Client: " + msg);
t.run();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void run()
{
while(true)
{
try
{
client = new Socket(serverName, port);
if(client.getInputStream()!=null){
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
tex.append("Server: " + in.readUTF() + "\n");}
client.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
@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_main, menu);
return true;
}
public void test(){
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
tex.setText("inside test");
/*String locationProvider = LocationManager.GPS_PROVIDER;
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
t.setText("Latitude: " + lastKnownLocation.getLatitude());*/
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
double d = location.getLatitude();
tex.setText("Latitude: " + d);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
这是空指针错误,我猜这是在run()方法中引起的。我已经尝试修复这个错误,因为现在已经超过几个小时,但都是徒劳的。
01-07 09:05:35.178 28413-28413/com.example.akash.myapplication W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4187fc08)
01-07 09:05:35.183 28413-28413/com.example.akash.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.akash.myapplication, PID: 28413
android.util.SuperNotCalledException: Activity {com.example.akash.myapplication/com.example.akash.myapplication.MainActivity} did not call through to super.onCreate()
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5603)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
我真的很感谢你帮助别人。 如果可能的话,请分享一些参考资料或指导我,我可以自己解决这些问题,并帮助这个伟大的社区中的其他人。 谢谢你提前。
更新后,跟随@M D
后错误现在变小了答案 0 :(得分:3)
在setContentView(R.layout.yourlayout);
初始化views
之前,您忘了onCreate(...)
。
答案 1 :(得分:1)
你的例子中很多东西都是非常错误的。
诚然,不是第一次NPE的原因,而是让我成为先知:
您可以在可运行的MainActivity的运行方法中访问tex
。
但是,tex
在onCreate
方法中初始化,这是android活动生命周期的一部分。当您致电Runnable r = new MainActivity();
时,您将在该生命周期之外创建一项新活动
tex
永远不会在新的MainActivity中初始化tex
也会是一个不同的视图,而不是你可能期望的,你会因为无限递归创建的MainActivities而创建一个BufferOverflow或OutOfMemoryError 如果你想让你的runnable成为MainActivity中的匿名实现,它可能会按照你想要的那样关闭tex
。
Runnable r = new Runnable {
public void run() {
try {
client = new Socket(serverName, port);
if(client.getInputStream()!=null){
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
tex.append("Server: " + in.readUTF() + "\n");}
client.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}