I am trying to pass the ListView postion that is being clicked to a new activity. When I run the app without trying to use the int in the next activity for instance trying to show it in a Toast message the app doesn't crash. But when I try and use the position integer to try and see if it works the app crashes.
Here is the code where I am passing the value:
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);
}
});
}
}
And here is the activity receiving the value:
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);
}
}
and here is the 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
CodePudding user response:
When you set text to an int variable, Android assumes you want the xt resource with that id. And that id doesn't happen to exist. To print an int as a string, use "" variable or Integer.toString(variable) to convert it to a string first.