如何传递点击通过Intent点击的列表项上的项目名称?这是对的吗?
public void onListItemClick(ListView l, View view, final int position, long id)
{ super.onListItemClick(l, view, positin, id);
Intent in = new Intent(MainActivity.this, PowerpointActiv.class);
in.putExtra("string", String.valueof(position));
startActivity(in);
}
然后在我的PowerpointActiv
中String nam = getIntent().getStringExtra("string");
String filepath = Environment.getExternalStorageDirectory().getPath() + "/" + nam;
我得到错误(java.runtimeExceptin:无法实例化活动ComponentInfo:Java.lang.nullpointerexception)..如何传递项目的String名称:(请帮助:(
答案 0 :(得分:1)
名称(在你的情况下为“string”)必须包含一个包前缀,例如app com.android.contacts将使用“com.android.contacts.string”之类的名称。
这是来自java doc:
public Intent putExtra (String name, String value)
Added in API level 1
Add extended data to the intent. The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".
Parameters
name The name of the extra data, with package prefix.
value The String data value.
Returns
Returns the same Intent object, for chaining multiple calls into a single statement
所以改变:
in.putExtra("string", String.valueof(position));
为:
in.putExtra("your.package.string", String.valueof(position));
和诅咒:
String nam = getIntent().getStringExtra("string");
为:
String nam = getIntent().getStringExtra("your.package.string");
我不知道这是不是你的问题,但这是一个开始