我的应用程序正在崩溃创建自定义列表视图片段

时间:2015-06-29 03:19:53

标签: android android-listfragment

当我尝试创建自定义列表视图片段时,我的应用程序崩溃了 我没有看到这方面的问题,但没有一个有帮助 这是我的HomeFragment.java:

public class HomeFragment extends Fragment implements AdapterView.OnItemClickListener {


    ArrayList<Questions> arrayQues;


    ListView listViewQues;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.custom_list_layout_home, container, false);

        arrayQues = new ArrayList<Questions>();

        //new LoadInbox().execute();
        Questions q1 = new Questions( "This is a sample question 1", 789456123, "28/06/2015");
        Questions q2 = new Questions( "This is a sample question 2", 789456123, "28/06/2015");
        Questions q3 = new Questions( "This is a sample question 3", 789456123, "28/06/2015");
        Questions q4 = new Questions( "This is a sample question 4", 789456123, "28/06/2015");
        Questions q5 = new Questions( "This is a sample question 5", 789456123, "28/06/2015");
        Questions q6 = new Questions( "This is a sample question 6", 789456123, "28/06/2015");
        Questions q7 = new Questions( "This is a sample question 7", 789456123, "28/06/2015");
        Questions q8 = new Questions( "This is a sample question 8", 789456123, "28/06/2015");
        Questions q9 = new Questions( "This is a sample question 9", 789456123, "28/06/2015");
        Questions q10 = new Questions( "This is a sample question 10", 789456123, "28/06/2015");
        Questions q11 = new Questions( "This is a sample question 11", 789456123, "28/06/2015");
        Questions q12 = new Questions( "This is a sample question 12", 789456123, "28/06/2015");
        Questions q13 = new Questions( "This is a sample question 13", 789456123, "28/06/2015");
        Questions q14 = new Questions( "This is a sample question 14", 789456123, "28/06/2015");

        arrayQues.add(q1);
        arrayQues.add(q2);
        arrayQues.add(q3);
        arrayQues.add(q4);
        arrayQues.add(q5);
        arrayQues.add(q6);
        arrayQues.add(q7);
        arrayQues.add(q8);
        arrayQues.add(q9);
        arrayQues.add(q10);
        arrayQues.add(q11);
        arrayQues.add(q12);
        arrayQues.add(q13);
        arrayQues.add(q14);

        perform(rootView);



        return rootView;
    }

    public void perform(View v) {
        listViewQues = (ListView)v.findViewById(R.id.list_ques);
        QuestionListAdapter adapter = new QuestionListAdapter(getActivity(), arrayQues);
        listViewQues.setAdapter(adapter);
        // Set the onItemClickListener on the ListView to listen for items clicks
        listViewQues.setOnItemClickListener( this);

        listViewQues.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                // ListView Clicked item index
                int itemPosition = position;

                // ListView Clicked item value
                String itemValue = (String) listViewQues.getItemAtPosition(position);

                // Show Alert
                Toast.makeText(getActivity().getApplicationContext(),
                        "Position :" + itemPosition + "  ListItem : " + itemValue, Toast.LENGTH_LONG)
                        .show();

            }

        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

给出了nullPoint异常

执行(rootView)和listViewQues.setAdapter(适配器);

这是我的QuestionListAdapter.java

package wolverine.example.com.btp_scientist;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Wolverine on 28-06-2015.
 */


public class QuestionListAdapter extends BaseAdapter {

    Context context;

    protected List<Questions> listQuestions;
    LayoutInflater inflater;

    public QuestionListAdapter(Context context, ArrayList<Questions> listQues) {
        this.listQuestions = listQuestions;
        this.inflater = LayoutInflater.from(context);
        this.context = context;
    }



    public int getCount() {
        return listQuestions.size();
    }

    public Questions getItem(int position) {
        return listQuestions.get(position);
    }

    @Override
    public long getItemId(int position) {
        return  listQuestions.indexOf(getItem(position));
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {

            holder = new ViewHolder();
            convertView = this.inflater.inflate(R.layout.custom_list_layout_home,
                    parent, false);

            holder.txtQues = (TextView) convertView
                    .findViewById(R.id.txt_questions);
            holder.txtDate = (TextView) convertView
                    .findViewById(R.id.txt_date);
            holder.txtNum = (TextView) convertView
                    .findViewById(R.id.txt_asked_by);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Questions question = listQuestions.get(position);
        holder.txtQues.setText(question.getQues());
        holder.txtNum.setText((int) question.getNumber());
        holder.txtQues.setText(question.getQues());

        return convertView;
    }

    private class ViewHolder {
        TextView txtQues;
        TextView txtNum;
        TextView txtDate;
    }

}

Logcat:

06-29 09:06:58.659  19195-19297/? E/Babel﹕ Network error while getting auth token
06-29 09:06:58.679  19195-19297/? E/Babel﹕ bfm: Cannot get auth token
            at f.a(Unknown Source)
            at bff.a(SourceFile:88)
            at bff.a(SourceFile:128)
            at bzp.a(SourceFile:289)
            at bzp.a(SourceFile:115)
            at bui.a(SourceFile:626)
            at bui.a(SourceFile:491)
            at bfq.a(SourceFile:39)
            at bfr.run(SourceFile:88)
     Caused by: java.io.IOException: NetworkError
            at dcr.a(Unknown Source)
            at f.a(Unknown Source)
            at bff.a(SourceFile:88)
            at bff.a(SourceFile:128)
            at bzp.a(SourceFile:289)
            at bzp.a(SourceFile:115)
            at bui.a(SourceFile:626)
            at bui.a(SourceFile:491)
            at bfq.a(SourceFile:39)
            at bfr.run(SourceFile:88)
06-29 09:06:59.309  19330-19330/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-29 09:06:59.409  16174-19328/? E/dalvikvm﹕ Could not find class 'android.security.KeyPairGeneratorSpec$Builder', referenced from method com.google.android.gms.auth.e.c.a
06-29 09:06:59.979  19195-19297/? E/Babel﹕ Network error while getting auth token
06-29 09:06:59.979  19195-19297/? E/Babel﹕ bfm: Cannot get auth token
            at f.a(Unknown Source)
            at bff.a(SourceFile:88)
            at bff.a(SourceFile:128)
            at bzp.a(SourceFile:289)
            at bzp.a(SourceFile:115)
            at bui.a(SourceFile:626)
            at bui.a(SourceFile:491)
            at bfq.a(SourceFile:39)
            at bfr.run(SourceFile:88)
     Caused by: java.io.IOException: NetworkError
            at dcr.a(Unknown Source)
            at f.a(Unknown Source)
            at bff.a(SourceFile:88)
            at bff.a(SourceFile:128)
            at bzp.a(SourceFile:289)
            at bzp.a(SourceFile:115)
            at bui.a(SourceFile:626)
            at bui.a(SourceFile:491)
            at bfq.a(SourceFile:39)
            at bfr.run(SourceFile:88)
06-29 09:07:00.389  16174-19348/? E/dalvikvm﹕ Could not find class 'android.os.UserHandle', referenced from method com.google.android.gms.icing.b.g.a
06-29 09:07:00.469  28484-19357/? E/SQLiteLog﹕ (5) statement aborts at 1: [PRAGMA journal_mode=TRUNCATE;]
06-29 09:07:00.559      156-495/? E/AudioHardwareMSM8660﹕ value of device and enable is 23 0 ALSA dev id:20
06-29 09:07:00.729  19330-19330/? E/dalvikvm﹕ Could not find class 'android.app.Notification$Action$Builder', referenced from method b.a
06-29 09:07:00.839  19330-19330/? E/dalvikvm﹕ Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method b.a
06-29 09:07:00.909  19039-19039/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{wolverine.example.com.btp_scientist/wolverine.example.com.btp_scientist.FinalScreen}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
            at android.app.ActivityThread.access$600(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:4787)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at wolverine.example.com.btp_scientist.HomeFragment.perform(HomeFragment.java:110)
            at wolverine.example.com.btp_scientist.HomeFragment.onCreateView(HomeFragment.java:100)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
            at android.app.Activity.performStart(Activity.java:5018)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2044)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
            at android.app.ActivityThread.access$600(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:4787)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)
06-29 09:07:01.089  19330-19330/? E/dalvikvm﹕ Could not find class 'android.app.Notification$Action$Builder', referenced from method b.a
06-29 09:07:01.139  19330-19330/? E/dalvikvm﹕ Could not find class 'android.transition.Transition', referenced from method b.a
06-29 09:07:01.139  19330-19330/? E/dalvikvm﹕ Could not find class 'android.transition.Transition', referenced from method b.a
06-29 09:07:01.139  19330-19330/? E/dalvikvm﹕ Could not find class 'android.transition.Transition', referenced from method b.a
06-29 09:07:01.199  19330-19330/? E/dalvikvm﹕ Could not find class 'android.app.RemoteInput[]', referenced from method b.a
06-29 09:07:01.229  19330-19330/? E/dalvikvm﹕ Could not find class 'android.transition.Transition', referenced from method b.b
06-29 09:07:01.769  19383-19383/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-29 09:07:02.049  19398-19398/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-29 09:07:02.229  19412-19412/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-29 09:07:02.569  28484-19435/? E/SQLiteLog﹕ (5) statement aborts at 1: [PRAGMA journal_mode=TRUNCATE;]
06-29 09:07:02.639  19436-19436/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-29 09:07:02.909  19452-19452/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-29 09:07:02.989      156-495/? E/AudioHardwareMSM8660﹕ write(): dec_id = 1 cur_rx = 23
06-29 09:07:02.989      156-495/? E/AudioHardwareMSM8660﹕ cur_rx for pcm playback = 23
06-29 09:07:02.989      156-495/? E/AudioHardwareMSM8660﹕ value of device and enable is 23 1 ALSA dev id:20
06-29 09:07:03.199      421-544/? E/InputDispatcher﹕ channel '435759e0 wolverine.example.com.btp_scientist/wolverine.example.com.btp_scientist.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
06-29 09:07:03.199      421-544/? E/InputDispatcher﹕ channel '43548c20 wolverine.example.com.btp_scientist/wolverine.example.com.btp_scientist.Registration (server)' ~ Channel is unrecoverably broken and will be disposed!
06-29 09:07:03.199      421-544/? E/InputDispatcher﹕ channel '418895f0 wolverine.example.com.btp_scientist/wolverine.example.com.btp_scientist.Register2ndStep (server)' ~ Channel is unrecoverably broken and will be disposed!
06-29 09:07:03.309  19465-19465/wolverine.example.com.btp_scientist E/Trace﹕ error opening trace file: No such file or directory (2)
06-29 09:07:03.389  19195-19297/? E/Babel﹕ Network error while getting auth token
06-29 09:07:03.389  19195-19297/? E/Babel﹕ bfm: Cannot get auth token
            at f.a(Unknown Source)
            at bff.a(SourceFile:88)
            at bff.a(SourceFile:128)
            at bzp.a(SourceFile:289)
            at bzp.a(SourceFile:115)
            at bui.a(SourceFile:626)
            at bui.a(SourceFile:491)
            at bfq.a(SourceFile:39)
            at bfr.run(SourceFile:88)
     Caused by: java.io.IOException: NetworkError
            at dcr.a(Unknown Source)
            at f.a(Unknown Source)
            at bff.a(SourceFile:88)
            at bff.a(SourceFile:128)
            at bzp.a(SourceFile:289)
            at bzp.a(SourceFile:115)
            at bui.a(SourceFile:626)
            at bui.a(SourceFile:491)
            at bfq.a(SourceFile:39)
            at bfr.run(SourceFile:88)
06-29 09:07:03.599  19482-19482/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-29 09:07:04.259  19436-19436/? E/dalvikvm﹕ Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
06-29 09:07:05.229      808-808/? E/libEGL﹕ eglSetBlobCacheFuncsANDROID resulted in an error: 0x300c
06-29 09:07:06.079      156-495/? E/AudioHardwareMSM8660﹕ value of device and enable is 23 0 ALSA dev id:20
06-29 09:07:06.509  28484-19546/? E/SQLiteLog﹕ (5) statement aborts at 1: [PRAGMA journal_mode=TRUNCATE;]
06-29 09:07:07.549  19592-19592/? E/Trace﹕ error opening trace file: No such file or directory (2)
06-29 09:07:07.679      421-544/? E/InputDispatcher﹕ channel '42ba5a30 wolverine.example.com.btp_scientist/wolverine.example.com.btp_scientist.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
06-29 09:07:07.839  28484-19605/? E/SQLiteLog﹕ (5) statement aborts at 1: [PRAGMA journal_mode=TRUNCATE;]
06-29 09:07:08.089  19482-19608/? E/dalvikvm﹕ Could not find class 'android.os.UserHandle', referenced from method com.amazon.identity.auth.device.framework.AndroidUser.<init>
06-29 09:07:08.199  19195-19297/? E/Babel﹕ Network error while getting auth token
06-29 09:07:08.209  19195-19297/? E/Babel﹕ bfm: Cannot get auth token
            at f.a(Unknown Source)
            at bff.a(SourceFile:88)
            at bff.a(SourceFile:128)
            at bzp.a(SourceFile:289)
            at bzp.a(SourceFile:115)
            at bui.a(SourceFile:626)
            at bui.a(SourceFile:491)
            at bfq.a(SourceFile:39)
            at bfr.run(SourceFile:88)
     Caused by: java.io.IOException: NetworkError
            at dcr.a(Unknown Source)
            at f.a(Unknown Source)
            at bff.a(SourceFile:88)
            at bff.a(SourceFile:128)
            at bzp.a(SourceFile:289)
            at bzp.a(SourceFile:115)
            at bui.a(SourceFile:626)
            at bui.a(SourceFile:491)
            at bfq.a(SourceFile:39)
            at bfr.run(SourceFile:88)

custom_list_layout_home.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="8dp"
    android:background="#ffffff">

    <RelativeLayout
        android:id="@+id/layout_item_car"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <LinearLayout
            android:id="@+id/layout_questions"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/txt_questions"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textStyle="normal" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/layout_car_color"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/layout_questions"
            android:orientation="horizontal"
            android:layout_marginTop="5dp">

            <TextView
                android:id="@+id/lbl_asked_by"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Asked By-"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textStyle="bold" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/txt_asked_by"/>

            <TextView
                android:id="@+id/txt_date"
                android:layout_width="wrap_content"
                android:layout_gravity="right"
                android:layout_marginLeft="100dp"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textStyle="normal" />
        </LinearLayout>

    </RelativeLayout>

</LinearLayout>

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/list_ques"
        android:layout_width="fill_parent"
        android:divider="@color/list_divider"
        android:dividerHeight="5dp"
        android:layout_height="wrap_content"
        android:listSelector="@drawable/list_row_selector"></ListView>

</RelativeLayout>

请帮助。提前致谢

2 个答案:

答案 0 :(得分:1)

使用

而不是

holder.txtNum.setText((int) question.getNumber());

你应该使用

holder.txtNum.setText(String.valueOf(question.getNumber()));

它将彻底解决你的崩溃!!

答案 1 :(得分:0)

您需要更改视图,

View rootView = inflater.inflate(R.layout.fragment_home, container, false);

请用 -

替换您的适配器方法
public QuestionListAdapter(Context context, ArrayList<Questions> listQues) {
        this.listQuestions = listQues;
        this.inflater = LayoutInflater.from(context);
        this.context = context;
    }

请使用getView()方法更改此内容,

holder.txtNum.setText(""+question.getNumber());