Java Android应用程序面临空指针异常
这是我的密码Java Android应用程序面临空指针异常,java,android,nullpointerexception,Java,Android,Nullpointerexception,这是我的密码 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 serverNa
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);
}
}
这里是null指针错误,我猜这是由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)
我真的很感激你帮助人们。
如果可能的话,请分享一些参考资料或指导我,我可以自己解决这些问题,也可以帮助这个伟大社区的其他人。
先谢谢你
更新后,在跟随@md之后,错误现在更小了您忘记了
setContentView(R.layout.yourlayout)代码>在初始化之前视图
在onCreate(…)
中您的示例中的许多内容都是非常错误的
诚然,这不是第一次NPE的原因,但让我做一个先知:
您可以在runnable Main活动的run方法中访问tex
但是,tex
是在onCreate
方法中初始化的,该方法是android活动生命周期的一部分。调用Runnable r=newmainActivity()时代码>您在该生命周期之外创建一个新活动,以便
tex
将永远不会在新的MainActivity中初始化
即使它会运行到onCreatetex
中,它也会是一个不同的视图,而不是您可能期望的视图,并且由于无限递归创建的main活动,您会创建一个BufferOverflow或OutOfMemoryError
如果您想在MainActivity中使您的runnable成为一个匿名实现,它可能会像您希望的那样关闭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();
}
}
}
我建议你先学习android教程。有很多错误,包括创建活动类newmainActivity()的实例代码>您应该替换Runnable r=newmainActivity();最终螺纹t=新螺纹(r)代码>到线程t=新线程(此)
@Raghunandan是的,我知道,我仍在学习android+编码,一只手让我有点懒(另一只手受伤并暂时残疾)。谢谢你的参考。@mr.icetea我认为这不会解决我的问题,是一个建议吗?你还需要调用super.onCreate(SavedValue)
内部一旦创建
错误现在就小了他,每个人都在关注主要活动
实现可运行
,却忽略了明显的错误。很高兴,谢谢你抽出时间。我照你说的做了,现在还有一个问题。01-07 09:27:47.698 31400-31400/com.example.akash.myapplication E/AndroidRuntime﹕ 致命异常:主进程:com.example.akash.myapplication,PID:31400 android.util.SuperNotCalledException:Activity{com.example.akash.myapplication/com.example.akash.myapplication.MainActivity}未调用super.onCreate()查看我对原始问题的评论