我有一个活动,我在其中显示通过 json 提供的列表项。当我单击该项目时,我想进行下一个活动并显示与我选择的特定项目名称相关的详细信息。我可以将所选项目名称显示为另一个页面上的标题,但它不显示其他内容,所以请帮我解决这个问题,我是 android 新手。提前致谢.. !!
这是我在 OnItemClickListener 上所做的代码:
locationList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String distance = ((TextView) view.findViewById(R.id.distance)).getText().toString();
String country = ((TextView) view.findViewById(R.id.country)).getText().toString();
String city = ((TextView) view.findViewById(R.id.city)).getText().toString();
String phone = ((TextView) view.findViewById(R.id.phone)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), LocationDetails.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_DISTANCE, distance);
in.putExtra(TAG_COUNTRY, country);
in.putExtra(TAG_CITY, city);
in.putExtra(TAG_PHONE, phone);
startActivity(in);
}
});
在这个我得到了名字,但在其他上它显示 null 不知道为什么我给它所有的参数都是真的......请帮助我。
公共类 LocationDetails 扩展 Activity{
// JSON node keys
private static final String TAG_NAME = "Name";
private static final String TAG_PHONE = "Phone";
private static final String TAG_ADDRESS = "Address";
private static final String TAG_DISTANCE= "Distance";
private static final String TAG_CITY = "City";
private static final String TAG_COUNTRY= "Country";
private TextView name,phone,address,distance,city,country;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location_details);
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
String Name = in.getStringExtra(TAG_NAME);
String Phone = in.getStringExtra(TAG_PHONE);
String Address = in.getStringExtra(TAG_ADDRESS);
String Distance = in.getStringExtra(TAG_DISTANCE);
String City = in.getStringExtra(TAG_CITY);
String Country = in.getStringExtra(TAG_COUNTRY);
// Displaying all values on the screen
name= (TextView) findViewById(R.id.TxtView_name);
phone=(TextView)findViewById(R.id.TxtView_PhoneField);
address=(TextView)findViewById(R.id.TxtView_AddressField);
distance=(TextView)findViewById(R.id.TxtView_DistanceField);
city=(TextView)findViewById(R.id.TxtView_CityField);
country=(TextView)findViewById(R.id.TxtView_CountryField);
name.setText(Name);
phone.setText(Phone);
address.setText(Address);
distance.setText(Distance);
city.setText(City);
country.setText(Country);
}
}
你告诉我我做到了,这发生了这是 logcat ......
04-13 13:01:25.810: D/dalvikvm(308): GC_FOR_MALLOC freed 14288 objects / 863832 bytes in 96ms
04-13 13:01:26.570: D/dalvikvm(308): GC_FOR_MALLOC freed 19218 objects / 1322032 bytes in 91ms
04-13 13:01:31.370: D/dalvikvm(308): GC_FOR_MALLOC freed 12650 objects / 889432 bytes in 87ms
04-13 13:01:31.880: I/MapActivity(308): Handling network change notification:CONNECTED
04-13 13:01:31.880: E/MapActivity(308): Couldn't get connection factory client
04-13 13:01:34.220: D/AndroidRuntime(308): Shutting down VM
04-13 13:01:34.220: W/dalvikvm(308): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-13 13:01:34.340: E/AndroidRuntime(308): FATAL EXCEPTION: main
04-13 13:01:34.340: E/AndroidRuntime(308): java.lang.NullPointerException
04-13 13:01:34.340: E/AndroidRuntime(308): at com.linkites.myperspace.Surrounding$1.onItemClick(Surrounding.java:164)
04-13 13:01:34.340: E/AndroidRuntime(308): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
04-13 13:01:34.340: E/AndroidRuntime(308): at android.widget.ListView.performItemClick(ListView.java:3382)
04-13 13:01:34.340: E/AndroidRuntime(308): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
04-13 13:01:34.340: E/AndroidRuntime(308): at android.os.Handler.handleCallback(Handler.java:587)
04-13 13:01:34.340: E/AndroidRuntime(308): at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 13:01:34.340: E/AndroidRuntime(308): at android.os.Looper.loop(Looper.java:123)
04-13 13:01:34.340: E/AndroidRuntime(308): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-13 13:01:34.340: E/AndroidRuntime(308): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 13:01:34.340: E/AndroidRuntime(308): at java.lang.reflect.Method.invoke(Method.java:521)
04-13 13:01:34.340: E/AndroidRuntime(308): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-13 13:01:34.340: E/AndroidRuntime(308): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-13 13:01:34.340: E/AndroidRuntime(308): at dalvik.system.NativeStart.main(Native Method)
04-13 13:01:37.020: I/Process(308): Sending signal. PID: 308 SIG: 9