Array Adapter,getView错误

时间:2018-04-23 17:51:08

标签: android listview arraylist baseadapter

我在浏览器上工作,正在研究创建历史记录部分。我已经设置了listview,它将显示已加载的所有网址。唯一的问题是,当我尝试在适配器的textview中设置getView()文本时,它会崩溃该应用。除了在运行时期间没有错误的迹象。 get item(部分)是一个char序列,我也将其转换为字符串,它仍会导致应用程序崩溃。我真的很想在应用程序中使用它,但我不知道在哪里寻找错误。

logcat的

04-23 13:42:10.354 5614-5614/com.example.mckenzieturner.cosmos_101E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mckenzieturner.cosmos_101, PID: 5614
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at com.example.mckenzieturner.cosmos_101.Methods.urlHistoryAdapter.getView(urlHistoryAdapter.java:57)
    at android.widget.AbsListView.obtainView(AbsListView.java:2406)
    at android.widget.ListView.makeAndAddView(ListView.java:1975)
    at android.widget.ListView.fillDown(ListView.java:709)
    at android.widget.ListView.fillFromTop(ListView.java:770)
    at android.widget.ListView.layoutChildren(ListView.java:1721)
    at android.widget.AbsListView.onLayout(AbsListView.java:2205)
    at android.view.View.layout(View.java:17564)
    at android.view.ViewGroup.layout(ViewGroup.java:5654)
    at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1197)
    at android.view.View.layout(View.java:17564)
    at android.view.ViewGroup.layout(ViewGroup.java:5654)
    at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1197)
    at android.view.View.layout(View.java:17564)
    at android.view.ViewGroup.layout(ViewGroup.java:5654)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:17564)
    at android.view.ViewGroup.layout(ViewGroup.java:5654)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
    at android.view.View.layout(View.java:17564)
    at android.view.ViewGroup.layout(ViewGroup.java:5654)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:17564)
    at android.view.ViewGroup.layout(ViewGroup.java:5654)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
    at android.view.View.layout(View.java:17564)
    at android.view.ViewGroup.layout(ViewGroup.java:5654)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at com.android.internal.policy.DecorView.onLayout(DecorView.java:725)
    at android.view.View.layout(View.java:17564)
    at android.view.ViewGroup.layout(ViewGroup.java:5654)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2406)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2126)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1286)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6536)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
    at android.view.Choreographer.doCallbacks(Choreographer.java:683)
    at android.view.Choreographer.doFrame(Choreographer.java:619)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6247)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

urlHistoryAdapter

public  class urlHistoryAdapter extends BaseAdapter {

private final Context context;
private final List<String> list = new ArrayList<>();


public urlHistoryAdapter(Context context) {
    this.context = context;

}

public void addItem(String item) {
    this.list.add(item);
}
public void deleteItem(String item) {
    this.list.remove(item);
}
public void deleteList(){
    list.removeAll(Collections.unmodifiableCollection(list));
}
@Override
public int getCount() {
    return list.size();
}
@Override
public String getItem(int position) {
    return list.get(position);
}
@Override
public long getItemId(int position) {
    return position;
}
@SuppressLint({"InflateParams", "ViewHolder"})
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    String Url = String.valueOf(getItem(position));
    View view = convertView;
        view = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);

    TextView textView = view.findViewById(R.id.title);
    textView.setText(getItem(position));


    return view;
}

}

1 个答案:

答案 0 :(得分:0)

尝试 -

LayoutInflater lf = LayoutInflater.from(parent.getContext());
    View vi = convertView;
    if (convertView == null) 
        vi = lf.inflate(R.layout.list_item, null);

同时检查您为TextView传递的ID是否正确,可能有一个错误...