android - 更改 startactivity 时出现 NullPointerException
问题描述
我想将我的活动 MainActivity 的开始活动更改为 TuserActivity。我在清单中声明了这些行:
<activity android:name=".TuserActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
但是我在运行应用程序时遇到了 NullPointerException,我该如何解决这个问题?
java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.krushi/com.example.krushi.TuserActivity}:java.lang.NullPointerException:尝试调用虚拟方法 'void android.widget.TextView.setOnClickListener(android. android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 的 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) 的空对象引用上的 view.View$OnClickListener)' android.app.ActivityThread.access $800(ActivityThread.java:151) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper .java:135) 在 android.app.ActivityThread.main(ActivityThread.java:5254) 在 java.lang。reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com. android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 原因:java.lang.NullPointerException:尝试调用虚拟方法 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on在 com.example.krushi.TuserActivity.onCreate(TuserActivity.java:44) 在 android.app.Activity.performCreate(Activity.java:5990) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 的空对象引用) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 698)原因:java.lang.NullPointerException:尝试在 com.example.krushi.TuserActivity.onCreate 的空对象引用上调用虚拟方法 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' (TuserActivity.java:44) 在 android.app.Activity.performCreate(Activity.java:5990) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:第2278章reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 698)原因:java.lang.NullPointerException:尝试在 com.example.krushi.TuserActivity.onCreate 的空对象引用上调用虚拟方法 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' (TuserActivity.java:44) 在 android.app.Activity.performCreate(Activity.java:5990) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:第2278章903) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 引起:java.lang.NullPointerException:尝试调用虚拟方法 'void android.widget.TextView.setOnClickListener(android.view.View $OnClickListener)' 在 com.example.krushi.TuserActivity.onCreate(TuserActivity.java:44) 在 android.app.Activity.performCreate(Activity.java:5990) 在 android.app.Instrumentation.callActivityOnCreate( Instrumentation.java:1106) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)903) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 引起:java.lang.NullPointerException:尝试调用虚拟方法 'void android.widget.TextView.setOnClickListener(android.view.View $OnClickListener)' 在 com.example.krushi.TuserActivity.onCreate(TuserActivity.java:44) 在 android.app.Activity.performCreate(Activity.java:5990) 在 android.app.Instrumentation.callActivityOnCreate( Instrumentation.java:1106) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)View$OnClickListener)' 在 com.example.krushi.TuserActivity.onCreate(TuserActivity.java:44) 在 android.app.Activity.performCreate(Activity.java:5990) 在 android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1106) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)View$OnClickListener)' 在 com.example.krushi.TuserActivity.onCreate(TuserActivity.java:44) 在 android.app.Activity.performCreate(Activity.java:5990) 在 android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1106) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
Tuser活动:
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class TuserActivity extends AppCompatActivity {
private EditText tphnumber;
Button btSend;
TextView tEmail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tuser);
tphnumber = findViewById(R.id.tphone);
btSend = findViewById(R.id.btSend);
btSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String mobile = tphnumber.getText().toString().trim();
if(mobile.isEmpty() || mobile.length() < 10){
tphnumber.setError("Enter a valid mobile");
tphnumber.requestFocus();
return;
}
Intent intent = new Intent(TuserActivity.this, VerifyPhoneActivity.class);
intent.putExtra("mobile", mobile);
startActivity(intent);
}
});
tEmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(TuserActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
}
主要活动:
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
public EditText emailId, passwd;
Button btnSignUp;
TextView signIn,txuser;
FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firebaseAuth = FirebaseAuth.getInstance();
emailId = findViewById(R.id.ETemail);
passwd = findViewById(R.id.ETpassword);
btnSignUp = findViewById(R.id.btnSignUp);
signIn = findViewById(R.id.TVSignIn);
txuser = findViewById(R.id.tuser);
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String emailID = emailId.getText().toString();
String paswd = passwd.getText().toString();
if (emailID.isEmpty()) {
emailId.setError("Enter your E-mail");
emailId.requestFocus();
} else if (paswd.isEmpty()) {
passwd.setError("Enter your password");
passwd.requestFocus();
} else if (emailID.isEmpty() && paswd.isEmpty()) {
Toast.makeText(MainActivity.this, "Fields Empty!", Toast.LENGTH_SHORT).show();
} else if (!(emailID.isEmpty() && paswd.isEmpty())) {
firebaseAuth.createUserWithEmailAndPassword(emailID, paswd).addOnCompleteListener(MainActivity.this, new OnCompleteListener() {
@Override
public void onComplete(Task task) {
if (!task.isSuccessful()) {
Toast.makeText(MainActivity.this.getApplicationContext(),
"SignUp unsuccessful: " + task.getException().getMessage(),
Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(MainActivity.this, UserActivity.class));
}
}
});
} else {
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
}
});
signIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent I = new Intent(MainActivity.this, Activity_Login.class);
startActivity(I);
}
});
txuser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent I = new Intent(MainActivity.this, TuserActivity.class);
startActivity(I);
}
});
}
}
解决方案
您必须在使用之前初始化tEmail 。
tEmail = findViewById(R.id.tEmail);
最终的初始化块应该像
tphnumber = findViewById(R.id.tphone);
btSend = findViewById(R.id.btSend);
tEmail = findViewById(R.id.tEmail);
推荐阅读
- firebase - Firebase 部署 - Flutter web - 未捕获类型错误:无法读取未定义的属性“isSupported”/无法为范围注册 ServiceWorker
- rust - syn::parse::ParseBuffer::peek() 如何接受类型作为参数?
- python - 如何比较箱线图中的所有行与某些行
- sql - 如何在 SQL 过程中创建视图
- python - slurm中如何从不同节点申请多个GPU?
- algorithm - 数据结构的建议
- azure - 在 MS Teams 中添加成员时,在 Azure Logic 应用程序中出现 403 Forbidden 错误
- c# - MessageHandler 不传递消息
- java - 让机器人在一个区域内向前移动
- markdown - 如何在使用 pandoc 时将 { 替换为 unicode 字符