My app in Android Studio does not run, it throws " java.lang.RuntimeException: Unable to instantiate activity"

huangapple go评论66阅读模式
英文:

My app in Android Studio does not run, it throws " java.lang.RuntimeException: Unable to instantiate activity"

问题

I understand your request. Here's the translated content without the code:

我在Android开发中是新手,我的应用程序出现了一些问题。我正在使用Android Studio构建一个简单的IMC(体重指数)计算器,它有两个活动页面。一个是主要的活动页面,您可以在其中输入所有数值(年龄、身高、体重等等),另一个页面会根据这些数值计算并显示结果。我在其他问题中看到,主要问题可能出现在清单文件(Manifest)中,下面是我的清单文件的内容。

清单文件(Manifest):

(清单文件内容已翻译)

我在MainActivity.java文件中定义了一些变量,这些变量名是用西班牙语命名的:

(Java代码已翻译)

确切的错误信息是:java.lang.RuntimeException: 无法实例化组件 ComponentInfo{com.example.calculoimc/com.example.calculoimc.MainActivity}: java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()'

我在另一个帖子中看到,我需要检查清单文件中的活动页面(activities)。我已经检查了清单文件中的活动页面,它们都已经正确声明,但我不知道该如何处理。如果有人能够帮助我,我将不胜感激。谢谢!

英文:

Im new in android development and im having some problem with my app, im building a simple IMC calculator in Android Studio, it has 2 activities, one is the main activity where you put all the values (age, height, weight...) and then another one that gives you the result taking as reference that values. I saw in other questions that the main problem could be in the manifest, here is my manifest.

Manifest:

`<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

&lt;application
    android:allowBackup=&quot;true&quot;
    android:dataExtractionRules=&quot;@xml/data_extraction_rules&quot;
    android:fullBackupContent=&quot;@xml/backup_rules&quot;
    android:icon=&quot;@mipmap/ic_launcher&quot;
    android:label=&quot;@string/app_name&quot;
    android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
    android:supportsRtl=&quot;true&quot;
    android:theme=&quot;@style/Theme.CalculoIMC&quot;
    tools:targetApi=&quot;34&quot;&gt;
    &lt;activity
        android:name=&quot;.SeeResults&quot;
        android:exported=&quot;false&quot;
        android:parentActivityName = &quot;com.example.calculoimc.MainActivity&quot;
  /&gt;
    &lt;activity android:name=&quot;com.example.calculoimc.MainActivity&quot;
        android:exported=&quot;true&quot;&gt;
        &lt;intent-filter&gt;
            &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
            &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;
&lt;/application&gt;

</manifest>`

MainActivity (Variables are in spanish):

package com.example.calculoimc;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    public void sendData (View v) {
        Intent i = new Intent(this,SeeResults.class);
        String [] info  = new String[4];
        info [0] = name;
        info [1] = edadN;
        info [2] = alturaN;
        info [3] = pesoN;
        i.putExtra(&quot;data&quot;,info);
        startActivity(i);
    }
    EditText nombreApellidos = findViewById(R.id.NombreApellidos);
    EditText edad = findViewById(R.id.Edad);
    EditText altura = findViewById(R.id.altura);
    EditText peso = findViewById(R.id.peso);

    /*DATOS*/
    String name = nombreApellidos.getText().toString().replace(&quot;Nombre&quot; , &quot;&quot;);
    String edadN = edad.getText().toString().replace(&quot;Edad&quot;,&quot;&quot;);
    String alturaN = altura.getText().toString().replace(&quot;Altura&quot;,&quot;&quot;);
    String pesoN = peso.getText().toString().replace(&quot;Peso&quot;,&quot;&quot;);

}

The exact error is : java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.calculoimc/com.example.calculoimc.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference

I saw in another post that i have to check the activities in the manifest, I tried checking the activities in the manifest and all of them are declared there correctly, i dont know what to do exactly, i would be grateful if somebody can help me, thanks

答案1

得分: 1

Sure, here is the translated code:

好的我已经解决了这相当简单问题出在代码的最后部分

EditText nombreApellidos = findViewById(R.id.NombreApellidos);
EditText edad = findViewById(R.id.Edad);
EditText altura = findViewById(R.id.altura);
EditText peso = findViewById(R.id.peso);

/*数据*/
String name = nombreApellidos.getText().toString().replace("名称", "");
String age = edad.getText().toString().replace("年龄", "");
String height = altura.getText().toString().replace("身高", "");
String weight = peso.getText().toString().replace("体重", "");

我将这些变量放在了方法sendData然后就没有任何问题了

解决方案

package com.example.calculoimc;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void sendData(View v) {
        EditText nombreApellidos = findViewById(R.id.NombreApellidos);
        EditText edad = findViewById(R.id.Edad);
        EditText altura = findViewById(R.id.altura);
        EditText peso = findViewById(R.id.peso);

        /*数据*/
        String name = nombreApellidos.getText().toString().replace("名称", "");
        String age = edad.getText().toString().replace("年龄", "");
        String height = altura.getText().toString().replace("身高", "");
        String weight = peso.getText().toString().replace("体重", "");
        Intent i = new Intent(this, SeeResults.class);
        String[] info = new String[4];
        info[0] = name;
        info[1] = age;
        info[2] = height;
        info[3] = weight;
        i.putExtra("数据", info);
        startActivity(i);
    }
}
英文:

Ok, i already solved it, it was pretty simple, the problem was the final part of the code:

EditText nombreApellidos = findViewById(R.id.NombreApellidos);
EditText edad = findViewById(R.id.Edad);
EditText altura = findViewById(R.id.altura);
EditText peso = findViewById(R.id.peso);
/*DATOS*/
String name = nombreApellidos.getText().toString().replace(&quot;Nombre&quot; , &quot;&quot;);
String edadN = edad.getText().toString().replace(&quot;Edad&quot;,&quot;&quot;);
String alturaN = altura.getText().toString().replace(&quot;Altura&quot;,&quot;&quot;);
String pesoN = peso.getText().toString().replace(&quot;Peso&quot;,&quot;&quot;);

I put the variables in the method "sendData" and then worked without any problem.

Solution:

package com.example.calculoimc;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendData (View v) {
EditText nombreApellidos = findViewById(R.id.NombreApellidos);
EditText edad = findViewById(R.id.Edad);
EditText altura = findViewById(R.id.altura);
EditText peso = findViewById(R.id.peso);
/*DATOS*/
String name = nombreApellidos.getText().toString().replace(&quot;Nombre&quot; , &quot;&quot;);
String edadN = edad.getText().toString().replace(&quot;Edad&quot;,&quot;&quot;);
String alturaN = altura.getText().toString().replace(&quot;Altura&quot;,&quot;&quot;);
String pesoN = peso.getText().toString().replace(&quot;Peso&quot;,&quot;&quot;);
Intent i = new Intent(this,SeeResults.class);
String [] info  = new String[4];
info [0] = name;
info [1] = edadN;
info [2] = alturaN;
info [3] = pesoN;
i.putExtra(&quot;data&quot;,info);
startActivity(i);
}
}

huangapple
  • 本文由 发表于 2023年6月27日 17:48:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563622.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定