ListView不显示文本自定义适配器

时间:2018-01-29 01:39:38

标签: java android listview android-adapter

您好我是Android开发的新手,我正在尝试使用自定义适配器在列表视图中显示一些文本,当我在手机上运行应用程序时崩溃......

这是我的Adapter类:

public class EarthquakeAdapter extends ArrayAdapter<Earthquake> {
    private TextView magnitude;
    private TextView location;
    private TextView date;

    public EarthquakeAdapter(@NonNull Context context, ArrayList<Earthquake> resource) {
        super(context, 0, resource);
    }

    public View getview(int position, View convertView, ViewGroup parent) {
        // Check if the existing view is being reused, otherwise inflate the view

        Earthquake CurrentEarthquake =  (Earthquake)getItem(position);

        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
        }

        magnitude = (TextView) listItemView.findViewById(R.id.magnitude);
        magnitude.setText((int) CurrentEarthquake.getMagnitude());

        location = listItemView.findViewById(R.id.location);
        location.setText(CurrentEarthquake.getLocation());

        date = listItemView.findViewById(R.id.date);
        date.setText(CurrentEarthquake.getDate());

        return listItemView;
    }
}

这是我的logcat:

    01-30 23:34:38.508 11110-11110/com.example.itis.quakereportapp 
    E/System: stat file error, path is 
    /data/app/com.example.itis.quakereportapp-1/lib/arm64, exception is 
    android.system.ErrnoException: stat failed: ENOENT (No such file or 
    directory)
    01-30 23:34:38.888 11110-11134/com.example.itis.quakereportapp E/GED: 
    Failed to get GED Log Buf, err(0)

                                                                      [ 
    01-30 23:34:38.888 11110:11134 I/         ]

    elapse(include ctx switch):4357 (ms), eglInitialize
    01-30 23:34:38.890 11110-11110/com.example.itis.quakereportapp 
    E/AndroidRuntime: FATAL EXCEPTION: main

    Process: com.example.itis.quakereportapp, PID: 11110

    android.content.res.Resources$NotFoundException: Resource ID #0x0

    at android.content.res.Resources.getValue(Resources.java:1490)

    at
 android.content.res.Resources.loadXmlResourceParser(Resources.java:3060)

    at android.content.res.Resources.getLayout(Resources.java:1264)

    at android.view.LayoutInflater.inflate(LayoutInflater.java:418)

    at 


 android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)

    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)

    at android.widget.AbsListView.obtainView(AbsListView.java:2572)

    at android.widget.ListView.makeAndAddView(ListView.java:1956)

    at android.widget.ListView.fillDown(ListView.java:757)

    at android.widget.ListView.fillFromTop(ListView.java:822)

    at android.widget.ListView.layoutChildren(ListView.java:1751)

    at android.widget.AbsListView.onLayout(AbsListView.java:2326)

    at android.view.View.layout(View.java:16121)

    at android.view.ViewGroup.layout(ViewGroup.java:5185)

    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)

    at 
    android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1948)
                                                                                     at android.widget.LinearLayout.onLayout(LinearLayout.java:1724)
                                                                                     at android.view.View.layout(View.java:16121)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5185)
                                                                                     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:639)
                                                                                     at android.widget.FrameLayout.onLayout(FrameLayout.java:574)
                                                                                     at android.view.View.layout(View.java:16121)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5185)
                                                                                     at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
                                                                                     at android.view.View.layout(View.java:16121)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5185)
                                                                                     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:639)
                                                                                     at android.widget.FrameLayout.onLayout(FrameLayout.java:574)
                                                                                     at android.view.View.layout(View.java:16121)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5185)
                                                                                     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
                                                                                     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)
                                                                                     at android.widget.LinearLayout.onLayout(LinearLayout.java:1722)
                                                                                     at android.view.View.layout(View.java:16121)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5185)
                                                                                     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:639)
                                                                                     at android.widget.FrameLayout.onLayout(FrameLayout.java:574)
                                                                                     at android.view.View.layout(View.java:16121)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5185)
                                                                                     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2532)
                                                                                     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2226)
                                                                                     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1323)
                                                                                     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6686)
                                                                                     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:800)
                                                                                     at android.view.Choreographer.doCallbacks(Choreographer.java:603)
                                                                                     at android.view.Choreographer.doFrame(Choreographer.java:572)
                                                                                     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:786)
                                                                                     at android.os.Handler.handleCallback(Handler.java:815)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                                     at android.os.Looper.loop(Looper.java:194)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5834)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1119)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)

我是Android开发的新手,我正在尝试使用自定义适配器在listview中显示一些文本,当我在手机上运行应用程序时崩溃...

2 个答案:

答案 0 :(得分:2)

我认为问题是magnitude.setText((int)CurrentEarthquake.getMagnitude());,因为setText Int String必须magnitude.setText(((int)CurrentEarthquake.getMagnitude())+"");,所以你可以写groupby希望帮助你。

答案 1 :(得分:0)

我知道这对我很愚蠢,但我发现了问题......

在我的适配器代码中使用了方法

  

getview

虽然它意味着

  

getView

我还了解了这个方法在这里的实际运作方式how getView works

谢谢大家的帮助