我的问题是当我运行应用程序并单击注册按钮时突然显示弹出窗口:“强制关闭”
这是我的代码:
Main.java
package com.example.server;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Main extends Activity {
private Button login,register,exit;
private EditText usertext,passtext;
public static String res="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
login=(Button) findViewById(R.id.login);
register=(Button) findViewById(R.id.register);
exit=(Button) findViewById(R.id.exit);
usertext=(EditText) findViewById(R.id.usertext);
passtext=(EditText) findViewById(R.id.passtext);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
login(usertext.getText().toString(),passtext.getText().toString());
}
});
register.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent re=new Intent(Main.this,register.class);
startActivity(re);
}
});
exit.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
finish();
}
});
}
public void login(String user,String pass) {
new loginserver("http://ujo.ir/login,php",user,pass).execute();
final ProgressDialog pd=new ProgressDialog(Main.this);
pd.setMessage("Loading...");
pd.show();
final Timer tm=new Timer();
tm.schedule(new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
if(!res.equals("")){
pd.cancel();
Toast.makeText(getApplicationContext(), res, Toast.LENGTH_LONG).show();
res="";
tm.cancel();
}
}
});
}
}, 1, 1000);
}
}
register.java
import java.io.ObjectOutputStream.PutField;
import java.util.Timer;
import java.util.TimerTask;
import com.example.server.R.layout;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class register extends Activity {
public static String res="";
private Button register,exit;
private EditText name,family,user,pass,email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
exit.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
finish();
}
});
register.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
register1(name.getText().toString(),family.getText().toString(),user.getText().toString(),pass.getText().toString(),email.getText().toString());
}
});
}
public void register1(String name,String family,String user,String pass,String email) {
new registerserver("http://ujo.ir/register.php", name, family, user, pass, email).execute();
final ProgressDialog pd=new ProgressDialog(this);
pd.setMessage("Loading...");
pd.show();
final Timer tm=new Timer();
tm.scheduleAtFixedRate(new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
if(!res.equals("")) {
pd.cancel();
Toast.makeText(getApplicationContext(), res, Toast.LENGTH_LONG).show();
res="";
tm.cancel();
}
}
});
}
}, 1, 1000);
}
}
registerserver.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import android.os.AsyncTask;
public class registerserver extends AsyncTask {
private String Name="";
private String Family="";
private String Link="";
private String User="";
private String Pass="";
private String Email="";
public registerserver(String link,String name,String family,String user,String pass,String email) {
name=Name;
family=Family;
link=Link;
user=User;
pass=Pass;
email=Email;
}
@Override
protected String doInBackground(Object... arg0) {
try {
String data=URLEncoder.encode("name","UTF8")+"="+URLEncoder.encode(Name,"UTF8");
data+="&"+URLEncoder.encode("family","UTF8")+"="+URLEncoder.encode(Family,"UTF8");
data+="&"+URLEncoder.encode("username","UTF8")+"="+URLEncoder.encode(User,"UTF8");
data+="&"+URLEncoder.encode("password","UTF8")+"="+URLEncoder.encode(Pass,"UTF8");
data+="&"+URLEncoder.encode("email","UTF8")+"="+URLEncoder.encode(Email,"UTF8");
data+="&"+URLEncoder.encode("status","UTF8")+"="+URLEncoder.encode("a","UTF8");
URL mylink=new URL(Link);
URLConnection connect=mylink.openConnection();
connect.setDoOutput(true);
OutputStreamWriter wr=new OutputStreamWriter(connect.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader reader=new BufferedReader(new InputStreamReader(connect.getInputStream()));
StringBuilder sb=new StringBuilder();
String line=null;
while((line=reader.readLine()) != null) {
sb.append(line);
}
register.res=sb.toString();
} catch (Exception e) {}
return "";
}
}
最后, LogCat
08-04 21:19:11.704: D/AndroidRuntime(2066): Shutting down VM
08-04 21:19:11.704: W/dalvikvm(2066): threadid=1: thread exiting with uncaught exception (group=0xa000f180)
08-04 21:19:11.704: E/AndroidRuntime(2066): FATAL EXCEPTION: main
08-04 21:19:11.704: E/AndroidRuntime(2066): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.server/com.example.server.register}: java.lang.NullPointerException
08-04 21:19:11.704: E/AndroidRuntime(2066): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-04 21:19:11.704: E/AndroidRuntime(2066): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-04 21:19:11.704: E/AndroidRuntime(2066): at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-04 21:19:11.704: E/AndroidRuntime(2066): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-04 21:19:11.704: E/AndroidRuntime(2066): at android.os.Handler.dispatchMessage(Handler.java:99)
08-04 21:19:11.704: E/AndroidRuntime(2066): at android.os.Looper.loop(Looper.java:137)
08-04 21:19:11.704: E/AndroidRuntime(2066): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-04 21:19:11.704: E/AndroidRuntime(2066): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 21:19:11.704: E/AndroidRuntime(2066): at java.lang.reflect.Method.invoke(Method.java:511)
08-04 21:19:11.704: E/AndroidRuntime(2066): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-04 21:19:11.704: E/AndroidRuntime(2066): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-04 21:19:11.704: E/AndroidRuntime(2066): at dalvik.system.NativeStart.main(Native Method)
08-04 21:19:11.704: E/AndroidRuntime(2066): Caused by: java.lang.NullPointerException
08-04 21:19:11.704: E/AndroidRuntime(2066): at com.example.server.register.onCreate(register.java:30)
08-04 21:19:11.704: E/AndroidRuntime(2066): at android.app.Activity.performCreate(Activity.java:4465)
08-04 21:19:11.704: E/AndroidRuntime(2066): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-04 21:19:11.704: E/AndroidRuntime(2066): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-04 21:19:11.704: E/AndroidRuntime(2066): ... 11 more
08-04 21:19:11.724: D/dalvikvm(2066): GC_CONCURRENT freed 147K, 3% free 6716K/6919K, paused 0ms+0ms
08-04 21:19:12.174: I/dalvikvm(2066): threadid=3: reacting to signal 3
08-04 21:19:12.174: I/dalvikvm(2066): Wrote stack traces to '/data/anr/traces.txt'
08-04 21:19:12.234: I/dalvikvm(2066): threadid=3: reacting to signal 3
08-04 21:19:12.234: I/dalvikvm(2066): Wrote stack traces to '/data/anr/traces.txt'
答案 0 :(得分:2)
您忘记在注册类活动中实现按钮“退出”和“注册”。这就是为什么你得到一个nullpointer异常。通过阅读日志,得出结论非常简单;)
在你继续前进之前,我会建议实现所有其他元素btw!