填充ListView时“应用程序已停止”

时间:2014-07-23 15:51:12

标签: java android eclipse listview

嗯,我有这个问题而且我不知道为什么,因为Eclipse并没有告诉我哪个错误。我是Java的初学者,我不知道该怎么做。这是我的代码:

package com.example.asado;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;

public class ListaActivity  extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set View to register.xml
    setContentView(R.layout.listas);
    addListenerOnButton();

    final ListView listview = (ListView) findViewById(R.id.lista_compra);
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
        "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
        "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
        "Android", "iPhone", "WindowsMobile" };

    final ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i < values.length; ++i) {
      list.add(values[i]);
    }
    final StableArrayAdapter adapter = new StableArrayAdapter(this,
        android.R.layout.simple_list_item_1, list);
    listview.setAdapter(adapter);

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

      @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override
      public void onItemClick(AdapterView<?> parent, final View view,
          int position, long id) {
        final String item = (String) parent.getItemAtPosition(position);
        view.animate().setDuration(2000).alpha(0)
            .withEndAction(new Runnable() {
              @Override
              public void run() {
                list.remove(item);
                adapter.notifyDataSetChanged();
                view.setAlpha(1);
              }
            });
      }

    });

}

private class StableArrayAdapter extends ArrayAdapter<String> {

    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

    public StableArrayAdapter(Context context, int textViewResourceId,
        List<String> objects) {
      super(context, textViewResourceId, objects);
      for (int i = 0; i < objects.size(); ++i) {
        mIdMap.put(objects.get(i), i);
      }
    }

    @Override
    public long getItemId(int position) {
      String item = getItem(position);
      return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
      return true;
    }

  }

  public void addListenerOnButton() {

    ImageButton compra = (ImageButton) findViewById(R.id.compra);

    // Listening to Login Screen link
    compra.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
                            // Cierro la ventana
            // cambio de pesta�a
            finish();
        }
    });
}
}

它有点乱,但错误是当这个布局即将打开时,我收到错误&#34;不幸的是应用程序停止了#34;。提前谢谢。

这些是Logcat的错误:

07-23 16:05:06.509: E/AndroidRuntime(8794): FATAL EXCEPTION: main
07-23 16:05:06.509: E/AndroidRuntime(8794): Process: com.example.asado, PID: 8794
07-23 16:05:06.509: E/AndroidRuntime(8794): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.example.asado/com.example.asado.ListaActivity}:   java.lang.NullPointerException
07-23 16:05:06.509: E/AndroidRuntime(8794):     at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
07-23 16:05:06.509: E/AndroidRuntime(8794):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
07-23 16:05:06.509: E/AndroidRuntime(8794):     at android.app.ActivityThread.access$800(ActivityThread.java:139)
07-23 16:05:06.509: E/AndroidRuntime(8794):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
07-23 16:05:06.509: E/AndroidRuntime(8794):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-23 16:05:06.509: E/AndroidRuntime(8794):     at android.os.Looper.loop(Looper.java:136)
07-23 16:05:06.509: E/AndroidRuntime(8794):     at android.app.ActivityThread.main(ActivityThread.java:5102)
07-23 16:05:06.509: E/AndroidRuntime(8794):     at java.lang.reflect.Method.invokeNative(Native Method)
07-23 16:05:06.509: E/AndroidRuntime(8794):     at java.lang.reflect.Method.invoke(Method.java:515)
07-23 16:05:06.509: E/AndroidRuntime(8794):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-23 16:05:06.509: E/AndroidRuntime(8794):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-23 16:05:06.509: E/AndroidRuntime(8794):     at dalvik.system.NativeStart.main(Native  Method)
07-23 16:05:06.509: E/AndroidRuntime(8794): Caused by: java.lang.NullPointerException
07-23 16:05:06.509: E/AndroidRuntime(8794):     at com.example.asado.ListaActivity.onCreate(ListaActivity.java:39)
07-23 16:05:06.509: E/AndroidRuntime(8794):     at android.app.Activity.performCreate(Activity.java:5248)
07-23 16:05:06.509: E/AndroidRuntime(8794):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
07-23 16:05:06.509: E/AndroidRuntime(8794):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
07-23 16:05:06.509: E/AndroidRuntime(8794):     ... 11 more

2 个答案:

答案 0 :(得分:0)

尝试替换:

@Override
    public long getItemId(int position) {
      String item = getItem(position);
      return mIdMap.get(item);
    }

用这个:

@Override
    public long getItemId(int position) {
      return position;
    }

答案 1 :(得分:0)

ListActivity的第39行有nullpointer查看列表是否设置正确,如果查看this example如果列表创建@android:id/list而不是通过给出id值创建,则列表效果会更好。

 <ListView
 android:id="@android:id/list"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" >
 </ListView>