首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android - Crash on setAdapter()

Android - Crash on setAdapter()
EN

Stack Overflow用户
提问于 2014-05-20 23:17:34
回答 1查看 224关注 0票数 0

当我为列表视图设置适配器时,我的应用程序崩溃。以下是我的代码:

MainActivity.java:

代码语言:javascript
运行
复制
package com.example.viewlist;

import android.app.Activity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.Arrays;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

    mainListView = (ListView) findViewById( R.id.mainListView );

    String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
                                      "Jupiter", "Saturn", "Uranus", "Neptune"};  
    ArrayList<String> planetList = new ArrayList<String>();
    planetList.addAll( Arrays.asList(planets) );

    listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);

    listAdapter.add( "Ceres" );
    listAdapter.add( "Pluto" );
    listAdapter.add( "Haumea" );
    listAdapter.add( "Makemake" );
    listAdapter.add( "Eris" );

    mainListView.setAdapter( listAdapter ); 

}
}

fragment_Main.xml:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ListView android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:id="@+id/mainListView">
</ListView>

simplerow.xml:

代码语言:javascript
运行
复制
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/rowTextView" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"
     android:padding="10dp"
     android:textSize="16sp" >
</TextView>

我使用的是Eclipse,困难的是我几乎以1:1的比例从源代码中复制了代码。由原作者从源码编译的apk正在运行。我不知道为什么,所以我需要你的帮助!提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2014-05-20 23:24:58

这些是什么?

代码语言:javascript
运行
复制
listAdapter.add( "Ceres" );
listAdapter.add( "Pluto" );
listAdapter.add( "Haumea" );
listAdapter.add( "Makemake" );
listAdapter.add( "Eris" );

删除这些文件,然后再次运行项目以查看它是否正常工作

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23763823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档