LayoutInflater麻烦

时间:2014-08-01 01:47:24

标签: java android listview baseadapter layout-inflater

在我的应用程序中,我从文件中读取数据,将其存储在arraylist中。 arraylist中的每个对象都包含两个数据。我想将每个数据显示为单独的TextView,每行为我创建的listView显示两个TextView。所以我创建了一个自定义BaseAdapter,但是我在实现View方法时遇到了困难。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
     Log.d("MyAdapter","getItemId successful");
     LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //LayoutInflater inflater = LayoutInflater.from(context);
    Log.d("MyAdapter","layout inflater successful");
    View row = inflater.inflate(R.layout.single_row,parent,false);
    Log.d("MyAdapter","view row successful");

    TextView Course = (TextView) row.findViewById(R.id.textView1);
    TextView Points = (TextView) row.findViewById(R.id.textView2);
    Row temp =rowList.get(position);
    Course.setText(temp.course);
    Points.setText(temp.points);
    /*for(int i =0; i<rowList.size();i++){
    Row r = rowList.get(i);
    course.setText(r.course);
    points.setText(r.points);
    }*/
    return row;

我尝试使用注释掉的for循环创建行,但它没有用,所以我尝试在YouTube上关注幻灯片视频(https://www.youtube.com/watch?v=_l9e2t4fcfM),但它对我不起作用。当我运行我的应用程序时,它不会过去

LayoutInflater inflater = LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

或者它会说“布局inflater成功”,因为Log语句。我相信错误与LayoutInflater有关,但我不知道如何修复它。请帮忙!

这是我的整个适配器类

class MyAdapter extends BaseAdapter{

    Context context;
    ArrayList<Row>rowList = new ArrayList<Row>();
//  LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    MyAdapter(Context c)
    {
        //String space = " ";
         Log.d("MyAdapter","adapting haha");
        FileInputStream fis = null;
        try {
            fis = c.openFileInput("GPACoursedata.txt");
            File courseDataFile = new File ("GPACoursedata.txt");
            if(courseDataFile.length()==0){

                CharSequence text = "Add Courses and grades";
                int duration = Toast.LENGTH_SHORT;

                Toast.makeText(c, text, duration).show();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        StringBuffer Cbuffer = new StringBuffer();
        int input = -1;
        try {
            while((input = fis.read())!=-1)
            {
                Cbuffer.append((char)input);
                //Cbuffer.append((char)input);
                 Log.d("MyAdapter","Cbuffer appending...");
            }
             Log.d("MyAdapter","Cbuffer check" + Cbuffer.toString());

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        try {
            fis = c.openFileInput("GPAPointsdata.txt");
            File courseDataFile = new File ("GPAPointsdata.txt");
            if(courseDataFile.length()==0){

                CharSequence text = " ";
                int duration = Toast.LENGTH_SHORT;

                Toast.makeText(c, text, duration).show();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        StringBuffer Pbuffer = new StringBuffer();
        input = -1;
        try {
            while((input = fis.read())!=-1)
            {
                Pbuffer.append((char)input);
                 Log.d("MyAdapter","Pbuffer appending...");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
            Log.d("MyAdapter","Pbuffer tinka " + Cbuffer.toString());
            Log.d("MyAdapter","Cbuffer " + Cbuffer.substring(0, Cbuffer.length()-1).toString());
            Log.d("MyAdapter","Pbuffer " + Pbuffer.substring(0, Pbuffer.length()-1).toString());

        }
        ArrayList<String>Lcourses = new ArrayList<String>();
        ArrayList<String>Lpoints = new ArrayList<String>();
        //int k = 0;
        //System.out.println(Cbuffer.);
        String cString = Cbuffer.substring(0,Cbuffer.length());
        Log.d("MyAdapter","substrings" + cString );
        int start =0;
        Log.d("MyAdapter","lastIndexOf" + String.valueOf(cString.lastIndexOf(" ")) );
        Log.d("MyAdapter","substrings testing" + cString.substring(0, 1) );

        while(cString.length()>1){
            start = cString.indexOf(" ") + 1;
            Lcourses.add(cString.substring(0, start));
            Log.d("MyAdapter","substrings 0 to start" + cString.substring(0, start ));
            cString=cString.substring(start,cString.length());
            Log.d("MyAdapter", "cString length "+String.valueOf(cString.length()));
            Log.d("MyAdapter","cStrings" + cString);
        }


        String pString = Cbuffer.substring(0,Pbuffer.length());
        Log.d("MyAdapter","p substrings" + pString );
        start =0;
        Log.d("MyAdapter","p IndexOf" + String.valueOf(pString.indexOf(" ")) );
        while(pString.length()>1){
            start = pString.indexOf(" ") + 1;
            Lpoints.add(pString.substring(0, start));
            Log.d("MyAdapter","substrings 0 to start" + pString.substring(0, start ));
            pString=pString.substring(start,pString.length());
            Log.d("MyAdapter", "pString length "+String.valueOf(pString.length()));
            Log.d("MyAdapter","pStrings" + pString);
            Log.d("MyAdapter","Lcourses.size" + String.valueOf(Lcourses.size()));
            Log.d("MyAdapter","Lpoints.size" + String.valueOf(Lpoints.size()));


        }
        for(int j =0;j<Lcourses.size();j++){
            Lcourses.get(j).replaceAll("$"," ");
            Log.d("MyAdapter","Done fixing the dollar signs");
            //rowList.add(new Row(Lcourses.get(j),Lpoints.get(j)));
            //Log.d("MyAdapter","Done adding to rowList");
        }
        for(int j =0;j<Lcourses.size();j++){
            //Lcourses.get(j).replaceAll("$"," ");
            //Log.d("MyAdapter","Done fixing the dollar signs");
            rowList.add(new Row(Lcourses.get(j),Lpoints.get(j)));
            Log.d("MyAdapter","Done adding to rowList");
        }

    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub

        return rowList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
         Log.d("MyAdapter","getCount successful");

        return rowList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
         Log.d("MyAdapter","getItem successful");

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
         Log.d("MyAdapter","getItemId successful");
         LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //LayoutInflater inflater = LayoutInflater.from(context);
        Log.d("MyAdapter","layout inflater successful");
        View row = inflater.inflate(R.layout.single_row,parent,false);
        Log.d("MyAdapter","view row successful");

        TextView course = (TextView) row.findViewById(R.id.textView1);
        TextView points = (TextView) row.findViewById(R.id.textView2);
        for(int i =0; i<rowList.size();i++){
        Row r = rowList.get(i);
        course.setText(r.course);
        points.setText(r.points);
        }
        return row;
    }

}

Log Cat

08-02 16:26:30.860: E/AndroidRuntime(4249): java.lang.NullPointerException: println needs a message
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.util.Log.println_native(Native Method)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.util.Log.d(Log.java:139)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at com.example.gpatestingapp.MyAdapter.getView(MainActivity.java:400)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.widget.AbsListView.obtainView(AbsListView.java:2263)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.widget.ListView.onMeasure(ListView.java:1175)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.view.View.measure(View.java:16497)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.view.View.measure(View.java:16497)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.view.View.measure(View.java:16497)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.view.View.measure(View.java:16497)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.view.View.measure(View.java:16497)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.view.View.measure(View.java:16497)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.view.Choreographer.doFrame(Choreographer.java:544)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.os.Handler.handleCallback(Handler.java:733)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.os.Handler.dispatchMessage(Handler.java:95)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.os.Looper.loop(Looper.java:136)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at android.app.ActivityThread.main(ActivityThread.java:5017)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at java.lang.reflect.Method.invokeNative(Native Method)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at java.lang.reflect.Method.invoke(Method.java:515)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-02 16:26:30.860: E/AndroidRuntime(4249):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

试试这个......

LayoutInflater inflater=((Activity)context).getLayoutInflater();