我放入自定义arrayadapter后listview崩溃

时间:2014-01-31 16:48:00

标签: java android listview android-listview

我最近开始为Android编程,我想制作一个程序,它实际上要复杂一点。

我遇到了一个与java超类如何工作有关的问题,我无法用我目前所知的方法调试它。

我编写了一个完美的代码,当我将listView放在默认的arrayadapter中时,但是当我尝试创建自定义arrayadapter时,它会在尝试刷新listView时立即崩溃。 以下是与自定义适配器相关的代码的一部分:

    private Button buttonNovoIme;
private ListView lvRadnici;
private ArrayList <String> listaRadnikaArray;
private StableArrayAdapter adapter;
private EditText novoIme;
private TextView buferZaIme;
private TextView errorText;



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


    buttonNovoIme= (Button) findViewById(R.id.buttonNovoIme);
    lvRadnici = (ListView) findViewById(R.id.listViewRadnici);
    listaRadnikaArray = new ArrayList<String>();
    novoIme = (EditText) findViewById(R.id.editTextNovoIme);
    errorText= (TextView) findViewById(R.id.errorText);

 final StableArrayAdapter adapter =  new StableArrayAdapter (this, R.layout.listview_podesavanje ,  listaRadnikaArray);
    lvRadnici.setAdapter(adapter);

 buttonNovoIme.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


            if (listaRadnikaArray.contains(novoIme.getText().toString()) || novoIme.getText().toString().matches("")) {


                    if (novoIme.getText().toString().matches("")) {
                            errorText.setText("prazno");    
                    }
                    else {
                        errorText.setText("duplikat");
                    }


            }

            else {
            listaRadnikaArray.add(novoIme.getText().toString());
            adapter.notifyDataSetChanged();
            errorText.setText("");
            }

            novoIme.setText("");
        }
    });

这是我的自定义arrayadapter,它是标准的make:

 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;
            }

          }

这是我的错误日志

01-31 15:04:06.385: E/AndroidRuntime(858): FATAL EXCEPTION: main
01-31 15:04:06.385: E/AndroidRuntime(858): java.lang.NullPointerException
01-31 15:04:06.385: E/AndroidRuntime(858):  at com.example.organizatorsmena.OrganizatorSmena$StableArrayAdapter.getItemId(OrganizatorSmena.java:263)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.widget.AbsListView.obtainView(AbsListView.java:2292)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.widget.ListView.onMeasure(ListView.java:1156)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.view.View.measure(View.java:15172)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.view.View.measure(View.java:15172)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.view.View.measure(View.java:15172)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.view.View.measure(View.java:15172)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-31 15:04:06.385: E/AndroidRuntime(858):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.view.View.measure(View.java:15172)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.view.Choreographer.doCallbacks(Choreographer.java:555)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.view.Choreographer.doFrame(Choreographer.java:525)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.os.Handler.handleCallback(Handler.java:615)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.os.Looper.loop(Looper.java:137)
01-31 15:04:06.385: E/AndroidRuntime(858):  at android.app.ActivityThread.main(ActivityThread.java:4745)
01-31 15:04:06.385: E/AndroidRuntime(858):  at java.lang.reflect.Method.invokeNative(Native Method)
01-31 15:04:06.385: E/AndroidRuntime(858):  at java.lang.reflect.Method.invoke(Method.java:511)
01-31 15:04:06.385: E/AndroidRuntime(858):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-31 15:04:06.385: E/AndroidRuntime(858):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-31 15:04:06.385: E/AndroidRuntime(858):  at dalvik.system.NativeStart.main(Native Method)

感谢。

2 个答案:

答案 0 :(得分:1)

您正在创建一个空的arrayList,然后将其提供给您的适配器:

listaRadnikaArray = new ArrayList<String>(); // this is empty
novoIme = (EditText) findViewById(R.id.editTextNovoIme);
errorText= (TextView) findViewById(R.id.errorText);
final StableArrayAdapter adapter =  new StableArrayAdapter (this, R.layout.listview_podesavanje ,  listaRadnikaArray);

在适配器中使用之前,需要在ArrayList中放置一些项目。

答案 1 :(得分:0)

好的我解决了这个问题。这是hashMap的错误。我不知道当你创建自定义的ArrayAdapter行时,构造函数不会在常规基础上刷新。

  public StableArrayAdapter(Context context, int textViewResourceId,
            List<String> objects) {
          super(context, textViewResourceId, objects);
          for (int i = 0; i < objects.size(); ++i) {  // this will not auto-refresh after 
            mIdMap.put(objects.get(i), i);            // you change items in ArrayList
          }

所以我需要做的是覆盖notifyDataSetChanged()

 public void  notifyDataSetChanged (List<String> newObject){
                super.notifyDataSetChanged();
                for (int i = 0; i < newObject.size(); ++i) {
                    mIdMap.put(newObject.get(i), i);

现在每次使用adapter.notifyDataSetChanged(arrayList)更新ArrayList时,它也会更新hashMap。