简体   繁体   English

Android:当我尝试从意图中检索数据时应用程序崩溃

[英]Android:App crashes when i try to retrieve data from intent

In activity 1:在活动 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);
            }
        });

    }
}

in Activity 2:在活动 2 中:

int val = getIntent().getIntExtra("val", 0); 

The debugger gives this:调试器给出了这个:

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

val is a number which is treated by makeText as a resource ID. val是一个数字, makeText将其视为资源 ID。 Turn it into a String: String.valueOf(val) .把它变成一个字符串: String.valueOf(val) Pass this expression to makeText instead if val itself.如果val本身,则将此表达式传递给makeText

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试重新登录时,Android应用程序崩溃 - Android app crashes when i try to reLogin 当我使用意图从另一个活动中检索数据时,应用程序崩溃 - Application crashes when I use intent to retrieve data from another activity 当我尝试从一种意图转到另一种意图时,我正在使用的应用程序崩溃了,我已经尝试了所有方法。 码: - The app I am working on crashes when i try to go from one intent to the other, I've tried everything. code: 当我尝试测试时,Android Studio应用程序崩溃 - Android Studio app crashes when i try to test it 当我尝试发送文本时,Android App崩溃 - Android App crashes when I try to send a text Android 应用程序在启动新 Intent 时崩溃 - Android app crashes when starting a new Intent 更改意图时,Android应用程序崩溃 - Android app crashes when changing intent 当我尝试从Firestore提取数据并显示在listview片段中时,我的应用程序崩溃 - My app crashes when i try to fetch data from Firestore and display in the listview fragment 当我尝试从片段切换到活动时,应用崩溃 - App crashes when I try to switch from fragment to activity 当我尝试使用intent传递对象时,它会崩溃我的应用程序 - When I try to pass object with intent it crashes my application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM