无法在Android Studio中启动活动componentInfo

发布于 2025-02-09 18:38:43 字数 1765 浏览 3 评论 0原文

我得到这个错误:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋叶绚丽 2025-02-16 18:38:43

取出该方法的元素并在类活动中没有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.

染火枫林 2025-02-16 18:38:43

NullPoInterException在尝试使用null类似的对象方法或变量时,就会发生: -

String name = null;
int len = name.length();

这也会抛出nullpoInterException。每个程序员都会经历这种错误,它非常普遍,解决此问题的最佳方法是读取错误,就像您在问题中共享的内容

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

一样

: IS 无法启动活动意味着在onCreate()方法中存在问题,因为第一个ongreate()方法是调用以启动任何活动。

第二行是尝试调用虚拟方法'void android.widget.button.setonClicklistener(android.view.view.view $ onclicklistener)'null对象参考,因为您可以阅读此尝试在< strong> null对象偏置这意味着您要打电话给单击null对象,表示按钮为null。

现在让我们了解为什么按钮为null,按以下原因可以按以下原因为null: -

  1. 按钮不是初始化(但您这样做)。
  2. 方法正在返回null,从中我们从中获取按钮findviewbyid()正在返回null。 (也许是一种情况)

现在为什么FindViewById()返回null,因为您可能放置了错误的ID,而FindViewById无法在视图中找到您的ID。

short检查您的视图ID和FindViewById()是因为
该FindViewById正在返回null。

我可以简短地说,但是我只想您自己解决问题,因为您将来会遇到错误。

感谢您的阅读

NullPointerException Occurs when you Attempt to use objects methods or variables which is null like :-

String name = null;
int len = name.length();

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:-

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

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:-

  1. button is not initialize ( But You did it ).
  2. method is returning null from which we get button from means findViewById() is returning null. ( It Maybe a case)

Now Why findViewById() is returning null because you may put wrong id and findViewById is not able to find your id in the View.

In-Short Check your View Id and findViewById() its the problem because
of that findViewById is returning null.

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

回眸一笑 2025-02-16 18:38:43

尝试此代码

遵循2件事 -

  • 在发送数据检查之前,请先使用ISEMPTY或NULL。
  • 在XML中添加InputType,以获取特定的EditTexts Ex。您的第二个递观是号码,所以请添加

android:inputType =“ number”

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);
        String number = (editText2.getText().toString());
       
        if (text.isEmpty() || number.isEmpty()) {
            Toast.makeText(this, "Please add inputs", Toast.LENGTH_LONG).show();
            return;
        }
        
        Intent intent = new Intent(this, Activity2.class);
        intent.putExtra(EXTRA_TEXT, text);
        intent.putExtra(EXTRA_NUMBER, number);
        startActivity(intent);
    }

}

Try this code

Follow 2 things-

  • Before sending data check isempty or null.
  • Add inputType in xml for particular editTexts Ex. your second EditText is Number so add

android:inputType="number"

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);
        String number = (editText2.getText().toString());
       
        if (text.isEmpty() || number.isEmpty()) {
            Toast.makeText(this, "Please add inputs", Toast.LENGTH_LONG).show();
            return;
        }
        
        Intent intent = new Intent(this, Activity2.class);
        intent.putExtra(EXTRA_TEXT, text);
        intent.putExtra(EXTRA_NUMBER, number);
        startActivity(intent);
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文