【问题标题】:App crashed after passing ListView position int to new activity将 ListView 位置 int 传递给新活动后,应用程序崩溃
【发布时间】:2022-01-03 04:56:36
【问题描述】:

我正在尝试将被单击的 ListView 位置传递给新活动。当我运行应用程序而不尝试在下一个活动中使用 int 时,例如尝试在 Toast 消息中显示它时,应用程序不会崩溃。但是当我尝试使用位置整数来尝试查看它是否有效时,应用程序崩溃了。

这是我传递值的代码:

package com.example.shashank.fffffffffffffffffffffffffff;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends AppCompatActivity {


    ListView flightList;

    ArrayAdapter customArrayAdapter;

    int intValue;



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

        flightList = findViewById(R.id.flightList);


        DBHelper dbHelper = new DBHelper(HomeActivity.this);
        List<FlightsModel> everyone = dbHelper.getEveryone();

        ArrayAdapter flightArrayAdapter = new ArrayAdapter<FlightsModel>(HomeActivity.this, android.R.layout.simple_list_item_1, everyone);
        flightList.setAdapter(flightArrayAdapter);

        flightList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {

                intValue = position;


                Intent intent = new Intent(HomeActivity.this, BookingActivity.class);

                intent.putExtra("intVariableName", intValue);

                startActivity(intent);
            }
        });


    }

}

这是接收值的活动:

package com.example.shashank.fffffffffffffffffffffffffff;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class BookingActivity extends AppCompatActivity {


    TextView textView;


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

        textView = findViewById(R.id.textView);



        Intent mIntent = getIntent();
        int intValue = mIntent.getIntExtra("intVariableName", 0);
        Toast.makeText(BookingActivity.this, "hello", Toast.LENGTH_SHORT).show();

        textView.setText(intValue);


    }


}

这里是 Logcat:

2021-11-24 21:41:18.053 11761-11761/com.example.shashank.fffffffffffffffffffffffffff W/System: ClassLoader referenced unknown path: /data/app/com.example.shashank.fffffffffffffffffffffffffff-2/lib/x86
2021-11-24 21:41:18.022 11761-11761/? I/art: Late-enabling -Xcheck:jni
2021-11-24 21:41:18.022 11761-11761/? W/art: Unexpected CPU variant for X86 using defaults: x86
    Process: com.example.shashank.fffffffffffffffffffffffffff, PID: 9870
2021-11-24 21:41:18.022 11761-11761/? I/art: Late-enabling -Xcheck:jni
2021-11-24 21:41:18.022 11761-11761/? W/art: Unexpected CPU variant for X86 using defaults: x86
2021-11-24 21:41:18.022 11761-11761/? I/art: Late-enabling -Xcheck:jni
2021-11-24 21:41:18.022 11761-11761/? W/art: Unexpected CPU variant for X86 using defaults: x86
2021-11-24 21:41:18.248 11761-11776/com.example.shashank.fffffffffffffffffffffffffff I/OpenGLRenderer: Initialized EGL, version 1.4
2021-11-24 21:41:18.249 11761-11776/com.example.shashank.fffffffffffffffffffffffffff D/OpenGLRenderer: Swap behavior 1
2021-11-24 21:41:18.249 11761-11776/com.example.shashank.fffffffffffffffffffffffffff W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2021-11-24 21:41:18.249 11761-11776/com.example.shashank.fffffffffffffffffffffffffff D/OpenGLRenderer: Swap behavior 0
2021-11-24 21:41:18.260 11761-11776/com.example.shashank.fffffffffffffffffffffffffff D/EGL_emulation: eglCreateContext: 0xb4405360: maj 2 min 0 rcv 2
2021-11-24 21:41:18.293 11761-11776/com.example.shashank.fffffffffffffffffffffffffff D/EGL_emulation: eglMakeCurrent: 0xb4405360: ver 2 0 (tinfo 0xb4403200)
2021-11-24 21:41:18.316 11761-11776/com.example.shashank.fffffffffffffffffffffffffff D/EGL_emulation: eglMakeCurrent: 0xb4405360: ver 2 0 (tinfo 0xb4403200)
2021-11-24 21:41:20.575 11761-11776/com.example.shashank.fffffffffffffffffffffffffff D/EGL_emulation: eglMakeCurrent: 0xb4405360: ver 2 0 (tinfo 0xb4403200)
2021-11-24 21:41:20.583 11761-11776/com.example.shashank.fffffffffffffffffffffffffff D/EGL_emulation: eglMakeCurrent: 0xb4405360: ver 2 0 (tinfo 0xb4403200)
2021-11-24 21:41:20.602 11761-11776/com.example.shashank.fffffffffffffffffffffffffff D/EGL_emulation: eglMakeCurrent: 0xb4405360: ver 2 0 (tinfo 0xb4403200)
2021-11-24 21:41:20.612 11761-11776/com.example.shashank.fffffffffffffffffffffffffff D/EGL_emulation: eglMakeCurrent: 0xb4405360: ver 2 0 (tinfo 0xb4403200)
2021-11-24 21:41:20.613 11761-11761/com.example.shashank.fffffffffffffffffffffffffff W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
2021-11-24 21:41:20.617 11761-11776/com.example.shashank.fffffffffffffffffffffffffff D/EGL_emulation: eglMakeCurrent: 0xb4405360: ver 2 0 (tinfo 0xb4403200)
2021-11-24 21:41:20.621 11761-11776/com.example.shashank.fffffffffffffffffffffffffff D/OpenGLRenderer: endAllActiveAnimators on 0x9738af80 (RippleDrawable) with handle 0x97371140
2021-11-24 21:41:21.095 11761-11776/com.example.shashank.fffffffffffffffffffffffffff D/EGL_emulation: eglMakeCurrent: 0xb4405360: ver 2 0 (tinfo 0xb4403200)
2021-11-24 21:41:23.062 11761-11761/com.example.shashank.fffffffffffffffffffffffffff W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
2021-11-24 21:41:25.655 11761-11776/com.example.shashank.fffffffffffffffffffffffffff D/EGL_emulation: eglMakeCurrent: 0xb4405360: ver 2 0 (tinfo 0xb4403200)
2021-11-24 21:41:25.683 11761-11776/com.example.shashank.fffffffffffffffffffffffffff D/EGL_emulation: eglMakeCurrent: 0xb4405360: ver 2 0 (tinfo 0xb4403200)
2021-11-24 21:41:25.895 11761-11776/com.example.shashank.fffffffffffffffffffffffffff D/EGL_emulation: eglMakeCurrent: 0xb4405360: ver 2 0 (tinfo 0xb4403200)
2021-11-24 21:41:25.912 11761-11776/com.example.shashank.fffffffffffffffffffffffffff D/EGL_emulation: eglMakeCurrent: 0xb4405360: ver 2 0 (tinfo 0xb4403200)
2021-11-24 21:41:25.921 11761-11776/com.example.shashank.fffffffffffffffffffffffffff D/EGL_emulation: eglMakeCurrent: 0xb4405360: ver 2 0 (tinfo 0xb4403200)
2021-11-24 21:41:25.926 11761-11761/com.example.shashank.fffffffffffffffffffffffffff W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
2021-11-24 21:41:25.933 11761-11776/com.example.shashank.fffffffffffffffffffffffffff D/OpenGLRenderer: endAllActiveAnimators on 0x96f66f80 (RippleDrawable) with handle 0x97371180
2021-11-24 21:41:32.579 11761-11764/com.example.shashank.fffffffffffffffffffffffffff I/art: Do partial code cache collection, code=24KB, data=30KB
2021-11-24 21:41:32.580 11761-11761/com.example.shashank.fffffffffffffffffffffffffff W/ResourceType: No package identifier when getting value for resource number 0x00000002
2021-11-24 21:41:32.581 11761-11761/com.example.shashank.fffffffffffffffffffffffffff D/AndroidRuntime: Shutting down VM
2021-11-24 21:41:32.581 11761-11761/com.example.shashank.fffffffffffffffffffffffffff E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.shashank.fffffffffffffffffffffffffff, PID: 11761
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shashank.fffffffffffffffffffffffffff/com.example.shashank.fffffffffffffffffffffffffff.BookingActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x2
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
     Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
        at android.content.res.Resources.getText(Resources.java:331)
        at android.widget.TextView.setText(TextView.java:4554)
        at com.example.shashank.fffffffffffffffffffffffffff.BookingActivity.onCreate(BookingActivity.java:29)
        at android.app.Activity.performCreate(Activity.java:6662)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6077) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
2021-11-24 21:41:32.582 11761-11764/com.example.shashank.fffffffffffffffffffffffffff I/art: After code cache collection, code=24KB, data=30KB
2021-11-24 21:41:32.582 11761-11764/com.example.shashank.fffffffffffffffffffffffffff I/art: Increasing code cache capacity to 128KB

【问题讨论】:

    标签: java android sqlite


    【解决方案1】:

    当您将 text 设置为 int 变量时,Android 会假定您需要具有该 ID 的 xt 资源。而且那个id并不存在。要将 int 打印为字符串,请先使用 ""+variable 或 Integer.toString(variable) 将其转换为字符串。

    【讨论】:

    • 谢谢你,成功了。不知道您不能将 int 值打印到 Toast/textViews。
    • 您可以,它只是将 int 解释为您的 strings.xml 文件中的值。其中 99.999% 的时间不会存在并崩溃。剩下的 .001% 你会得到一个非常随机的字符串。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 2019-06-02
    • 1970-01-01
    • 2015-07-28
    • 1970-01-01
    相关资源
    最近更新 更多