我正在开发一个简单的Android应用程序。我想在单击列表项时使用另一个活动显示listview属性。但我突然犯了错误。
这是我的java类,
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
// Main class for creating job_id lists
public class TourNumberList extends ListActivity{
TourNumberListAdapter tourNumberListAdapter;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.tour_list);
TourInfoDatabaseHelper jobInfoDatabaseHelper = new TourInfoDatabaseHelper(this);
Cursor cursorTourNumberInfo = jobInfoDatabaseHelper.getAllJobInfoRecords();
if(cursorTourNumberInfo.moveToFirst()){
do{
String jobId = cursorTourNumberInfo.getString(0);
String startLocation = cursorTourNumberInfo.getString(1);
String endLocation = cursorTourNumberInfo.getString(2);
String type = cursorTourNumberInfo.getString(3);
String tourNumber = cursorTourNumberInfo.getString(5);
android.util.Log.d("DB value", jobId+" "+startLocation+" "+endLocation+" "+type+" "+tourNumber);
}while(cursorTourNumberInfo.moveToNext());
}
ListView listView = (ListView) findViewById(R.id.tourList);
tourNumberListAdapter = new TourNumberListAdapter(this, cursorTourNumberInfo);
listView.setAdapter(tourNumberListAdapter);
// Get listview
ListView lv = getListView();
// on seleting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String startLocation = ((TextView) view.findViewById(R.id.start_location_invisible)).getText().toString();
String endLocation = ((TextView) view.findViewById(R.id.end_location_invisible)).getText().toString();
String type = ((TextView) view.findViewById(R.id.type_invisible)).getText().toString();
String tourNumber = ((TextView) view.findViewById(R.id.tour_number_invisible)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
TourInfo.class);
// sending pid to next activity
in.putExtra("startLocation", startLocation);
in.putExtra("endLocation", endLocation);
in.putExtra("type", type);
in.putExtra("tourNumber", tourNumber);
// starting new activity and expecting some response back
startActivity(in);
}
});
}
}
我正在使用CursorAdapter for listView,
public class TourNumberListAdapter extends CursorAdapter{
public TourNumberListAdapter(Context context, Cursor cursor) {
super(context, cursor);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView tourNumTextView = (TextView) view.findViewById(R.id.job_id);
tourNumTextView.setText(cursor.getString(0));
TextView startLocationInvisibleTextView = (TextView) view.findViewById(R.id.start_location_invisible);
startLocationInvisibleTextView.setText(cursor.getString(1));
TextView endLocationInvisibleTextView = (TextView) view.findViewById(R.id.end_location_invisible);
endLocationInvisibleTextView.setText(cursor.getString(2));
TextView typeInvisibleTextView = (TextView) view.findViewById(R.id.type_invisible);
typeInvisibleTextView.setText(cursor.getString(3));
TextView tourNumberInvisibleTextView = (TextView) view.findViewById(R.id.tour_number_invisible);
tourNumberInvisibleTextView.setText(cursor.getString(4));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.tour_list_item,parent,false);
return view;
}
}
这是我的listView的.xml文件,
<?xml version="1.0" encoding="UTF-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tourList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
这是我的Logcat错误,
04-01 14:56:38.003: E/AndroidRuntime(5765): FATAL EXCEPTION: main
04-01 14:56:38.003: E/AndroidRuntime(5765): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tourinfo/com.example.tourinfo.TourNumberList}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-01 14:56:38.003: E/AndroidRuntime(5765): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-01 14:56:38.003: E/AndroidRuntime(5765): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-01 14:56:38.003: E/AndroidRuntime(5765): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-01 14:56:38.003: E/AndroidRuntime(5765): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-01 14:56:38.003: E/AndroidRuntime(5765): at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 14:56:38.003: E/AndroidRuntime(5765): at android.os.Looper.loop(Looper.java:123)
04-01 14:56:38.003: E/AndroidRuntime(5765): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-01 14:56:38.003: E/AndroidRuntime(5765): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 14:56:38.003: E/AndroidRuntime(5765): at java.lang.reflect.Method.invoke(Method.java:507)
04-01 14:56:38.003: E/AndroidRuntime(5765): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-01 14:56:38.003: E/AndroidRuntime(5765): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-01 14:56:38.003: E/AndroidRuntime(5765): at dalvik.system.NativeStart.main(Native Method)
04-01 14:56:38.003: E/AndroidRuntime(5765): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-01 14:56:38.003: E/AndroidRuntime(5765): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
04-01 14:56:38.003: E/AndroidRuntime(5765): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:210)
04-01 14:56:38.003: E/AndroidRuntime(5765): at android.app.Activity.setContentView(Activity.java:1657)
04-01 14:56:38.003: E/AndroidRuntime(5765): at com.example.tourinfo.TourNumberList.onCreate(TourNumberList.java:20)
04-01 14:56:38.003: E/AndroidRuntime(5765): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-01 14:56:38.003: E/AndroidRuntime(5765): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-01 14:56:38.003: E/AndroidRuntime(5765): ... 11 more
请帮我解决此错误。
答案 0 :(得分:3)
试试这个..
您使用ListActivity
然后在布局中ListView
的ID应为@android:id/list
<?xml version="1.0" encoding="UTF-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
并删除ListView listView = (ListView) findViewById(R.id.tourList);
然后
// Get listview
ListView lv = getListView();
tourNumberListAdapter = new TourNumberListAdapter(this, cursorTourNumberInfo);
setListAdapter(tourNumberListAdapter);
lv.setOnItemClickListener(new OnItemClickListener() {
答案 1 :(得分:0)
将您的listview的ID更改为@android:id/list
所以使用这个
<?xml version="1.0" encoding="UTF-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
而不是
<?xml version="1.0" encoding="UTF-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tourList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>