在我向程序中添加一些新代码之前它运行正常。我想添加一个listView来显示一些消息,但它不起作用。当应用程序进入此活动时,程序崩溃... 我补充说:
String[] studentList = {"zhangsan","lisi"};
ListView stdListView = (ListView)findViewById(R.id.studentListView);
ArrayAdapter<String> arrayAdapter;
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_2,studentList);
stdListView.setAdapter(arrayAdapter);
stdListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String result = parent.getItemAtPosition(position).toString();
AlertDialog.Builder choice = new AlertDialog.Builder(EditActivity.this);
choice.setTitle(result);
}
});
为:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
String[] studentList = {"zhangsan","lisi"};
ListView stdListView = (ListView)findViewById(R.id.studentListView);
ArrayAdapter<String> arrayAdapter;
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_2,studentList);
stdListView.setAdapter(arrayAdapter);
stdListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String result = parent.getItemAtPosition(position).toString();
AlertDialog.Builder choice = new AlertDialog.Builder(EditActivity.this);
choice.setTitle(result);
}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
以下是布局的xml文件。 我补充道:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/studentListView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
为:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="n402lab.door.EditActivity"
tools:showIn="@layout/app_bar_edit">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/studentListView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
答案 0 :(得分:0)
在我看到关于这个问题的评论之后,我试着自己调试它。在此之前,我还不知道如何调试应用程序。 我看到&#34;您必须为TextView提供资源ID&#34; 我搜索解决方案,最后我替换了#34; android.R.layout.simple_list_item_2&#34; by&#34; android.R.layout.simple_list_item_1&#34; 它的确有效。非常感谢!