Android - 在setAdapter()上崩溃

时间:2014-05-20 15:17:34

标签: android eclipse adapter

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

MainActivity.java:

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:

<?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:

<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的比例复制了代码。由orig作者编译的apk正在运行。我不知道为什么,所以我需要你的帮助! 提前谢谢!

1 个答案:

答案 0 :(得分:4)

这些是什么?

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

删除这些并再次运行项目以查看它是否有效