Nikhil Kumar Gupta Nikhil Kumar Gupta - 5 months ago 19
Android Question

Resources$NotFoundException when trying to extract data from intent

SO I am trying to create a contact manager module for a much bigger Android app, and I am using a Fragment in a ViewPager to display the contacts in a ListView. What I now want is the user to be able to edit the record by longClicking an item and selecting "Edit" from a List AlertDialog. The thing is, I want the new ContactEdit activity to have all the fields for the columns in the table filled with info of the particular record that was longClicked in the ListView of previous Fragment. I used the putExtra method to put in the Row ID of the record in the intent I am using to open the Activity. But on the other end, when I used the getIntent().getExtras().getInt() method, I was returned a android.content.res.Resources$NotFoundException. Please take a look at the relevant code and error log and try to help out a learner bro here..

ContactList.class

package com.example.nikhil.test1;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;



public class ContactList extends Fragment {
private SimpleCursorAdapter dataAdapter;
private ContactsAdapter dbHelper;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_item_list, container, false);
dbHelper = new ContactsAdapter(this.getActivity());
dbHelper.open();
Cursor cursor = dbHelper.getAllFriends();
String[] columns = new String[] {
ContactsAdapter.KEY_NAME, ContactsAdapter.KEY_Phone
};
int[] to = new int[] {
R.id.name, R.id.phonenumber
};
dataAdapter = new SimpleCursorAdapter(
this.getActivity(), R.layout.contact_row,
cursor,
columns,
to,
0);
ListView listView = (ListView) rootView.findViewById(R.id.list);
listView.setAdapter(dataAdapter);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Pick an action");
builder.setItems(new String[]{"Edit","Delete","Activate Call Forwarding"}, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if(which==0){
Intent i = new Intent(getActivity(), ContactEdit.class);
i.putExtra("RowID",position);
startActivity(i);
}

}
});
AlertDialog alertDialog=builder.create();
alertDialog.show();
return true;
}
});
return rootView;
}


}


ContactEdit.class

package com.example.nikhil.test1;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.widget.EditText;


public class ContactEdit extends Activity {
ContactsAdapter dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_edit);
dbHelper = new ContactsAdapter(this);
dbHelper.open();


if(getIntent() != null) {
Bundle extras = getIntent().getExtras();
EditText _Name = (EditText) findViewById(R.id.edittextName);
_Name.setText(extras.getInt("RowID"));
/* int rowId = extras != null ? extras.getInt("RowID") : -1;
Cursor cursor = dbHelper.getFriend(rowId);
String name = cursor.getString(1);
String mobile = cursor.getString(2);
String home = cursor.getString(3);
String address = cursor.getString(4);

EditText _Name = (EditText) findViewById(R.id.edittextName);
_Name.setText(name);
EditText _Mobile = (EditText) findViewById(R.id.edittextMobile);
_Mobile.setText(mobile);
EditText _Home = (EditText) findViewById(R.id.edittextHome);
_Home.setText(home);
EditText _Address = (EditText) findViewById(R.id.edittextAddress);
_Address.setText(address);*/
}
else
{
EditText _Name = (EditText) findViewById(R.id.edittextName);
_Name.setText(" ");
EditText _Mobile = (EditText) findViewById(R.id.edittextMobile);
_Mobile.setText(" ");
EditText _Home = (EditText) findViewById(R.id.edittextHome);
_Home.setText(" ");
EditText _Address = (EditText) findViewById(R.id.edittextAddress);
_Address.setText(" ");
}


}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {

NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}


I put the actual intended code in comment so that I could see if even my Activity was able to extract the RowID from the intent
logat error log

06-18 15:49:45.676 18965-18965/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.nikhil.test1, PID: 18965
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nikhil.test1/com.example.nikhil.test1.ContactEdit}: android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2335)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2397)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5268)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.content.res.Resources.getText(Resources.java:305)
at android.widget.TextView.setText(TextView.java:4152)
at com.example.nikhil.test1.ContactEdit.onCreate(ContactEdit.java:23)
at android.app.Activity.performCreate(Activity.java:6033)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2397)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5268)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)

Answer

this line may be causing the error:

  _Name.setText(extras.getInt("RowID"));

replace it with:

 _Name.setText(""+extras.getInt("RowID"));

just added "" to convert the integer to a String. Try it and tell us.

Comments