gpt4 book ai didi

java - 我放入自定义数组适配器后 ListView 崩溃

转载 作者:行者123 更新时间:2023-12-01 13:32:27 28 4
gpt4 key购买 nike

我最近开始为android编程,我想制作一个稍微复杂一点的程序,以供练习。

我遇到了一个与java父类(super class)如何工作相关的问题,我无法用我目前所知道的来调试它。

当我将 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("");
}
});

这是我的自定义数组适配器,它是标准品牌:

 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)

谢谢。

最佳答案

您正在创建一个空的 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 之前,您需要将一些项目放入其中。

关于java - 我放入自定义数组适配器后 ListView 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21485482/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com