Android:当我尝试从意图中检索数据时应用程序崩溃
Posted
技术标签:
【中文标题】Android:当我尝试从意图中检索数据时应用程序崩溃【英文标题】:Android:App crashes when i try to retrieve data from intent 【发布时间】:2022-01-01 01:56:16 【问题描述】:在活动 1 中:
package com.example.project1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
private Button button1, button2;
private RadioGroup rd;
private RadioButton checkmass, checktemp, checklen;
public int val = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.bt1);
button2 = (Button) findViewById(R.id.bt2);
rd = findViewById(R.id.rg1);
RadioButton checkMass, checkTemp, checkLen;
checkMass = findViewById(R.id.rb1);
checkLen = findViewById(R.id.rb2);
checkTemp = findViewById(R.id.rb3);
Intent intent1 = new Intent(this, MainActivity2.class);
Intent intent2 = new Intent(this, MainActivity3.class);
button1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if(checkMass.isChecked() || checkLen.isChecked() || checkTemp.isChecked())
if(checkMass.isChecked())
val = 1;
else if(checkLen.isChecked())
val = 2;
else if(checkTemp.isChecked())
val = 3;
intent1.putExtra("val",val);
Toast.makeText(MainActivity.this, val, Toast.LENGTH_SHORT).show();
openActivity2();
else
Toast.makeText(MainActivity.this, "Choose one of the three choices", Toast.LENGTH_SHORT).show();
private void openActivity2()
intent1.putExtra(isc, val);
startActivity(intent1);
);
button2.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
openActivity3();
private void openActivity3()
startActivity(intent2);
);
在活动 2 中:
int val = getIntent().getIntExtra("val", 0);
调试器给出这个:
E/xample.project: Invalid ID 0x00000001.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.project1, PID: 26709
android.content.res.Resources$NotFoundException: String resource ID #0x1
at android.content.res.Resources.getText(Resources.java:348)
at android.widget.Toast.makeText(Toast.java:307)
at com.example.project1.MainActivity$1.onClick(MainActivity.java:48)
at android.view.View.performClick(View.java:6597)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/Process: Sending signal. PID: 26709 SIG: 9
【问题讨论】:
请发布您的 MainActivity 课程 @WhatAJerk 完成 【参考方案1】:val
是一个数字,makeText
将其视为资源 ID。把它变成一个字符串:String.valueOf(val)
。如果 val
本身,则将此表达式传递给 makeText
。
【讨论】:
以上是关于Android:当我尝试从意图中检索数据时应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章
我无法检索应在 Android 上的意图数据内的 Firebase 动态链接
例外:从意图存储ArrayList时“无法转换为java.util.ArrayList”
android 从 MySql 数据库中检索 blob 图像