我想获得一个额外的字符串,但我收到此错误,我无法弄清楚问题出在哪里:
以下是我如何添加额外内容:
Intent i = new Intent(Hoteli.this, ListingDetails.class);
i.putExtra(hoteli.get(position).getImage(), "listing_image");
我是如何得到额外的东西:
Bundle extras = getIntent().getExtras();
String imgURL=extras.getString("listing_image");
Log.v("--", imgURL);
这是我的LogCat输出:
06-02 13:51:27.561: E/AndroidRuntime(535): FATAL EXCEPTION: main
06-02 13:51:27.561: E/AndroidRuntime(535): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.biznismap.com/com.biznismap.com.activities.ListingDetails}: java.lang.NullPointerException: println needs a message
06-02 13:51:27.561: E/AndroidRuntime(535): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-02 13:51:27.561: E/AndroidRuntime(535): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-02 13:51:27.561: E/AndroidRuntime(535): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-02 13:51:27.561: E/AndroidRuntime(535): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-02 13:51:27.561: E/AndroidRuntime(535): at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 13:51:27.561: E/AndroidRuntime(535): at android.os.Looper.loop(Looper.java:123)
06-02 13:51:27.561: E/AndroidRuntime(535): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-02 13:51:27.561: E/AndroidRuntime(535): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 13:51:27.561: E/AndroidRuntime(535): at java.lang.reflect.Method.invoke(Method.java:521)
06-02 13:51:27.561: E/AndroidRuntime(535): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-02 13:51:27.561: E/AndroidRuntime(535): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-02 13:51:27.561: E/AndroidRuntime(535): at dalvik.system.NativeStart.main(Native Method)
06-02 13:51:27.561: E/AndroidRuntime(535): Caused by: java.lang.NullPointerException: println needs a message
06-02 13:51:27.561: E/AndroidRuntime(535): at android.util.Log.println_native(Native Method)
06-02 13:51:27.561: E/AndroidRuntime(535): at android.util.Log.v(Log.java:101)
06-02 13:51:27.561: E/AndroidRuntime(535): at com.biznismap.com.activities.ListingDetails.getExtras(ListingDetails.java:34)
06-02 13:51:27.561: E/AndroidRuntime(535): at com.biznismap.com.activities.ListingDetails.init(ListingDetails.java:51)
06-02 13:51:27.561: E/AndroidRuntime(535): at com.biznismap.com.activities.ListingDetails.onCreate(ListingDetails.java:28)
06-02 13:51:27.561: E/AndroidRuntime(535): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-02 13:51:27.561: E/AndroidRuntime(535): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-02 13:51:27.561: E/AndroidRuntime(535): ... 11 more
答案 0 :(得分:1)
i.putExtra(hoteli.get(position).getImage(), "listing_image");
第一个参数是关键而不是值:
i.putExtra("listing_image", hoteli.get(position).getImage());
另外,由于你没有使用bundle来放置你的listen_image,但你将它嵌入到Intent中,你应该这样检索它:
String imgURL = getIntent(). getStringExtra("listing_image");
答案 1 :(得分:1)
Intent i = new Intent(Hoteli.this, ListingDetails.class);
i.putExtra(hoteli.get(position).getImage(), "listing_image");
应该是
Intent i = new Intent(Hoteli.this, ListingDetails.class);
i.putExtra("listing_image", hoteli.get(position).getImage());
//1st paramter is the key
//2nd parameter is the value
获得
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("listing_image");
//get the value based on the key
// key should match
}
答案 2 :(得分:0)
您在put extra方法中给出错误的键和值更改键值
i.putExtra("KEY","Value");
i.putExtra("listing_image",hoteli.get(position).getImage() );