无法在Android Studio中启动活动componentInfo
我得到这个错误:
ntime:致命例外:主要 过程:com.example.puzzle_project,pid:4100 java.lang.runtimeException:无法启动活动componentInfo {com.example.puzzle_project/com.example.puzzle_project.mainactivity}:java.lang.nullpointerexception: view.view $ onclicklistener)'null对象引用
但我不知道问题是什么。
我的主要目的是:
package com.example.puzzle_project;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_TEXT="com.example.application.example.EXTRA_TEXT";
public static final String EXTRA_NUMBER="com.example.application.example.EXTRA_NUMBER";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openActivity2();
}
});
}
public void openActivity2(){
EditText editText1= (EditText) findViewById(R.id.edittext1);
String text= editText1.getText().toString();
EditText editText2=(EditText)findViewById(R.id.edittext2);
int number = Integer.parseInt(editText2.getText().toString());
Intent intent = new Intent(this, Activity2.class);
intent.putExtra(EXTRA_TEXT,text);
intent.putExtra(EXTRA_NUMBER,number);
startActivity(intent);
}
}
i get this error:
ntime: FATAL EXCEPTION: main
Process: com.example.puzzle_project, PID: 4100
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.puzzle_project/com.example.puzzle_project.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
but i dont know what is the problem.
my mainActivity is:
package com.example.puzzle_project;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_TEXT="com.example.application.example.EXTRA_TEXT";
public static final String EXTRA_NUMBER="com.example.application.example.EXTRA_NUMBER";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openActivity2();
}
});
}
public void openActivity2(){
EditText editText1= (EditText) findViewById(R.id.edittext1);
String text= editText1.getText().toString();
EditText editText2=(EditText)findViewById(R.id.edittext2);
int number = Integer.parseInt(editText2.getText().toString());
Intent intent = new Intent(this, Activity2.class);
intent.putExtra(EXTRA_TEXT,text);
intent.putExtra(EXTRA_NUMBER,number);
startActivity(intent);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
取出该方法的元素并在类活动中没有FindViewByid初始化它们,并且在方法onclick()中使用“ FindViewById”,并将这些元素作为方法openActivity2()的参数传递。
我没有尝试过,但我认为是问题。
take out the elements of the method and initialize them without findviewbyid in the class activity and in the method onClick() use "findviewbyid" for your element and pass these elements as parameters of the method openActivity2().
i have not try but i think is the probleme.
NullPoInterException
在尝试使用null类似的对象方法或变量时,就会发生: -这也会抛出
nullpoInterException
。每个程序员都会经历这种错误,它非常普遍,解决此问题的最佳方法是读取错误,就像您在问题中共享的内容一样
: IS
无法启动活动
意味着在onCreate()方法中存在问题,因为第一个ongreate()方法是调用以启动任何活动。第二行是
尝试调用虚拟方法'void android.widget.button.setonClicklistener(android.view.view.view $ onclicklistener)'null对象参考
,因为您可以阅读此尝试在< strong> null对象偏置这意味着您要打电话给单击null对象,表示按钮为null。现在让我们了解为什么按钮为null,按以下原因可以按以下原因为null: -
现在为什么FindViewById()返回null,因为您可能放置了错误的ID,而FindViewById无法在视图中找到您的ID。
我可以简短地说,但是我只想您自己解决问题,因为您将来会遇到错误。
感谢您的阅读
NullPointerException
Occurs when you Attempt to use objects methods or variables which is null like :-This will also throw
NullPointerException
. Every programmer go through this type of Error its very common and the best way to solve this problem is to read the error like what you share in the Question is:-Lets try to understand this error what it is trying to say so,
First Line is
Unable to start activity
means there is a problem in the OnCreate() Method because First OnCreate() method is call to start any Activity.Second Line is
Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
As you can read this Attempt to invoke method on NULL OBJECT REFRENCE It's means you are call on click on null objects means button is null.Now lets Understand Why button is null, button can be null by following reasons like:-
Now Why findViewById() is returning null because you may put wrong id and findViewById is not able to find your id in the View.
I can say that in short way but I just want you to solve your problem on your own because your are going to get a hell of errors in the future.
Thanks for reading
尝试此代码
遵循2件事 -
Try this code
Follow 2 things-