gpt4 book ai didi

java - 如何修复无法启动 Activity ComponentInfo?

转载 作者:行者123 更新时间:2023-12-02 04:28:40 28 4
gpt4 key购买 nike

我的问题是当我运行应用程序并单击注册按钮时突然显示弹出窗口:“强制关闭”

这是我的代码:

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);
}
}

注册.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'

最佳答案

您忘记在注册类 Activity 中实例化按钮“退出”和“注册”。这就是为什么你会得到空指针异常。通过阅读日志,很容易得出这个结论;)

在继续之前,我建议实例化所有其他元素!

关于java - 如何修复无法启动 Activity ComponentInfo?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31819642/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com