英文:
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">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CalculoIMC"
tools:targetApi="34">
<activity
android:name=".SeeResults"
android:exported="false"
android:parentActivityName = "com.example.calculoimc.MainActivity"
/>
<activity android:name="com.example.calculoimc.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</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("data",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("Nombre" , "");
String edadN = edad.getText().toString().replace("Edad","");
String alturaN = altura.getText().toString().replace("Altura","");
String pesoN = peso.getText().toString().replace("Peso","");
}
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("Nombre" , "");
String edadN = edad.getText().toString().replace("Edad","");
String alturaN = altura.getText().toString().replace("Altura","");
String pesoN = peso.getText().toString().replace("Peso","");
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("Nombre" , "");
String edadN = edad.getText().toString().replace("Edad","");
String alturaN = altura.getText().toString().replace("Altura","");
String pesoN = peso.getText().toString().replace("Peso","");
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("data",info);
startActivity(i);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论