模型类在第二次使用时未将特定字符串传递给ListViewAdapter

时间:2018-09-11 04:44:36

标签: java android

这是模型类

public class Video {
        private String videoTitle;
        private String videoThumb;
        private String videoChannel;
        private String videoID;

        // Constructor to convert JSON object into a Java class instance
        public Video(JSONObject object) {
            try {
                JSONObject obj = object.getJSONObject("snippet");

                this.videoTitle = obj.getString("title");

                JSONObject obj3 = obj.getJSONObject("thumbnails").getJSONObject("maxres");
                this.videoThumb = obj3.getString("url");

                this.videoChannel = obj.getString("channelTitle");

    //This is the Variable Which is not getting passed in other fragment
                this.videoID = "eHarS-r_CC4";

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        public String getVideoTitle() { return videoTitle; }
        public String getVideoThumb() {
            return videoThumb;
        }
        public String getVideoChannel() {return videoChannel; }
        public String getVideoID() {return videoID; }
    }

这是Adapter类:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = convertView;
        final ViewHolderVideo holder;
        if (view == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            view = inflater.inflate(resource, parent, false);

        Log.d("videoTitle", video.getVideoTitle());

        final String videoID = video.getVideoID();
        Log.d("videoID", videoID);
}

不是当我在HomeFragment中使用Model类和适配器时,它为所有列表项传递了videoTitle和videoID,但是当我在其他片段中使用相同的Model类和Adapter时,它传递了videoTitle,但将videoID抛出以下错误。

*FATAL EXCEPTION: main
                                                                                 Process: com.example.shubi.myapplication, PID: 11808
                                                                                 java.lang.NullPointerException: println needs a message
                                                                                     at android.util.Log.println_native(Native Method)
                                                                                     at android.util.Log.d(Log.java:143)
                                                                                     at **net.moodsense.moodsense.adapter.CustomListViewVideoAdapter.getView(CustomListViewVideoAdapter.java:93)**
//This is the line where I call to show videoID in Logcat
                                                                                     at android.widget.AbsListView.obtainView(AbsListView.java:2404)
                                                                                     at android.widget.ListView.makeAndAddView(ListView.java:2052)
                                                                                     at android.widget.ListView.fillDown(ListView.java:786)
                                                                                     at android.widget.ListView.fillFromTop(ListView.java:847)
                                                                                     at android.widget.ListView.layoutChildren(ListView.java:1798)
                                                                                     at android.widget.AbsListView.onLayout(AbsListView.java:2203)
                                                                                     at android.view.View.layout(View.java:19692)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:6057)
                                                                                     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                                     at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                                     at android.view.View.layout(View.java:19692)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:6057)
                                                                                     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                                     at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                                     at android.view.View.layout(View.java:19692)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:6057)
                                                                                     at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1858)
                                                                                     at android.view.View.layout(View.java:19692)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:6057)
                                                                                     at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1171)
                                                                                     at android.view.View.layout(View.java:19692)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:6057)
                                                                                     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                                     at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                                     at android.view.View.layout(View.java:19692)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:6057)
                                                                                     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
                                                                                     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
                                                                                     at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
                                                                                     at android.view.View.layout(View.java:19692)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:6057)
                                                                                     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                                     at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                                     at android.view.View.layout(View.java:19692)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:6057)
                                                                                     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
                                                                                     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
                                                                                     at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
                                                                                     at android.view.View.layout(View.java:19692)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:6057)
                                                                                     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:759)
                                                                                     at android.view.View.layout(View.java:19692)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:6057)
                                                                                     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2521)
                                                                                     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2230)
                                                                                     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1416)
                                                                                     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6845)
                                                                                     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)
                                                                                     at android.view.Choreographer.doCallbacks(Choreographer.java:778)
                                                                                     at android.view.Choreographer.doFrame(Choreographer.java:713)
                                                                                     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:952)
                                                                                     at android.os.Handler.handleCallback(Handler.java:789)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                                     at android.os.Looper.loop(Looper.java:164)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:6798)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
09-11 09:41:29.

796 11808-11808 / com.example.shubi.myapplication D / OSTracker:操作系统事件:崩溃*

2 个答案:

答案 0 :(得分:0)

您需要使用BaseAdapter类的findsetTag方法。

getTag

答案 1 :(得分:0)

用这个替换您的getter方法。我已经修改了

 public String getVideoTitle() { return this.videoTitle; }
    public String getVideoThumb() {
        return this.videoThumb;
    }
    public String getVideoChannel() {return this.videoChannel; }
    public String getVideoID() {return this.videoID; }

希望这会对您有所帮助