所以我正在尝试为更大的Android应用程序创建一个联系人管理器模块,我在ViewPager中使用Fragment来显示ListView中的联系人。我现在想要的是用户能够通过长按一个项目并选择"编辑"来编辑记录。来自List AlertDialog。问题是,我希望新的ContactEdit活动使表中列的所有字段都填充了以前Fragment的ListView中longClicked的特定记录的信息。我使用putExtra方法在我用来打开Activity的intent中放入记录的Row ID。但另一方面,当我使用getIntent()。getExtras()。getInt()方法时,我返回了一个android.content.res.Resources $ NotFoundException。请查看相关代码和错误日志,并尝试在这里帮助学习者..
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);
}
}
我将实际的目标代码放在注释中,这样我就可以看到我的Activity是否能够从intent中提取RowID logat错误日志
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)
答案 0 :(得分:1)
此行可能导致错误:
while (!stopProgram)
{
String line = "";
System.out.print("> ");
System.out.flush();
line = keyboard.next();
System.out.println(line);
if ("close".equals(line))
{
entry.stop = true;
stopProgram = true;
}
}
将其替换为:
printf()
刚添加“”将整数转换为String。试试并告诉我们。