我正在关注制作列表视图的教程,但我找不到错误的原因
mainlistview1无法解析或不是字段。
这是我目前的代码:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.os.Build;
import android.widget.AdapterView.*;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
String[] riverCities = new String[] {"Greenville", "Vicksburg",
"Memphis", "Natchez", "Cape Girardeau", "Helena"};
ListView listView = (ListView)findViewById(R.id.mainListView1);
ListAdapter adapter = new ArrayAdapter<String>(this, android.R.id.mainListView1, riverCities);
listView.setAdapter(adapter);
}
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ListView
android:id="@+id/mainListView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ListView>
</LinearLayout>
答案 0 :(得分:0)
<强>问题:强>
android.R.id.mainListView1
mainListView1
不在android默认布局ID中,因此编译器无法找到它。
<强>溶液强>
将其更改为默认列表视图项ID
ListAdapter adapter = new ArrayAdapter<String>(this ,android.R.layout.simple_list_item_1, riverCities);