使用ListView与Fragment无法正常工作

时间:2013-06-10 14:05:39

标签: android listview

我有一个左右片段的页面。左侧片段工作正常,因为我没有使用Listview。我只是使用TextView来显示5个项目。在右侧片段中,我必须显示学生信息列表,但数量可能会有所不同。因此,我正在尝试使用Listview。代码编译没有错误,但它在运行时抛出异常。我在逻辑上做错了,我无法弄明白。我已经阅读了两次ListView文档,仍然无法弄清楚我做错了什么。 对你的帮助表示感谢?

student_row.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="match_parent" >
<ScrollView 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
      <RelativeLayout 
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          >

        <TextView
        android:id="@+id/tvStudentNum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="2dp"
        android:text="CMR"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/tvStudentFName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/tvStudentNum"
        android:layout_marginLeft="5dp"
        android:text="Service"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/tvStudentLname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/tvStudentFName"
        android:layout_marginLeft="5dp"
        android:text="Description"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/tvStudentAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/tvStudentLname"
        android:layout_marginLeft="5dp"
        android:text="Responsible Official"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/tvStartDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/tvStudentAddress"
        android:layout_marginLeft="5dp"
        android:text="Reconcile Date"
        android:textAppearance="?android:attr/textAppearanceLarge" />



</ScrollView>


</LinearLayout>



// Today Right Frame Screen. The today_right_fragment.xml that is supposed to replace the other frame



 <?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="match_parent"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/lvRightFrame"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>

    </LinearLayout>

// TodayRight Fragment

package com.sample.student;

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

import com.sample.android.student.adapter.StudentAdapter;
import com.sample.android.student.asynctask.StudentAsyncTask;
import com.sample.student.studentclasses.student;

import android.app.Fragment;
//import android.app.ListFragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class TodayRightFragment extends Fragment {
  private String[] session;
  private ListView lvStudent; 
  Context context;
  private Student[] student = null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.i("Starting the OnCreate Activity", "Activity");
        return inflater.inflate(R.layout.today_right_fragment, container, false);
        //  return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Log.i("Starting the OnCreate Activity", "Activity");
        Intent intent = getActivity().getIntent();
        session = intent.getStringArrayExtra("loginInfo");
        StudentAsyncTask studentAsyncTask = new StudentAsyncTask();
        //student = new ArrayList<Student>();
        try {
            student = studentAsyncTask.execute(session[0]).get();
            context = getActivity();
            lvStudent; = (ListView) getView().findViewById(R.id.lvRightFrame);
                StudentAdapter adapter = new                StudentAdapter(context,R.layout.student_row, student);
            lvStudent.setAdapter(adapter);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.i("CMR Error Message ",e.getMessage());
        }
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        Log.i("Starting the OnCreate Activity", "Activity");
        //  InitializeVariable();
        super.onStart();
    }


}

//学生适配器

package com.dss.android.apar.adapter;

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

import com.sample.student.studentclasses.student;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class StudentAdapter extends ArrayAdapter<Student> {
  private Context context;
  private int layoutResourceId;
  private Student[] student = null;


    public CRMAdapter(Context context, int layoutResourceId,Student[] student) {
        super(context,layoutResourceId, student);
        this.context = context;
        this.layoutResourceId = layoutResourceId;
        this.student = student;
    }

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

        if(row != null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent,false);
            holder = new StudenttemHolder();
            holder.tvStudentNum = (TextView) row.findViewById(R.id.tvStudentNum);
            holder.tvStudentFName = (TextView) row.findViewById(R.id.tvStudentFName);
            holder.tvStudentLname = (TextView) row.findViewById(R.id.tvStudentLname);
            holder.tvStudentAddress = (TextView) row.findViewById(R.id.tvStudentAddress);
            holder.tvStartDate = (TextView) row.findViewById(R.id.tvStartDate);

            row.setTag(holder);
        } else
        {
            holder = (StudenttemHolder) row.getTag();
        }

        Student item = Student[position];
        holder.tvStudentNum.setText(item.GetStudentNum());
        holder.tvStudentFName.setText(item.GetStudentFName());
        holder.tvStudentLname.setText(item.GetStudentLname());
        holder.tvStudentAddress.setText(item.GetStudentAddress());
        holder.tvStartDate.setText(item.GetStartDate());


        return row;
    }

    static class StudenttemHolder
    {
        TextView tvStudentNum;
        TextView tvStudentFName;
        TextView tvStudentLname;
        TextView tvStudentAddress;
        TextView tvStartDate;

    }
}

以下是摘要:

E/AndroidRuntime( 2426): FATAL EXCEPTION: main

E/AndroidRuntime( 2426): java.lang.NullPointerException

E/AndroidRuntime( 2426):    at com.sample.android.student.adapter.StudentAdapter.getView(StudentAdapter.java:46)

E/AndroidRuntime( 2426):    at android.widget.AbsListView.obtainView(AbsListView.java:2159)

E/AndroidRuntime( 2426):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)

E/AndroidRuntime( 2426):    at android.widget.ListView.onMeasure(ListView.java:1158)

E/AndroidRuntime( 2426):    at android.view.View.measure(View.java:15518)

E/AndroidRuntime( 2426):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)

E/AndroidRuntime( 2426):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)

E/AndroidRuntime( 2426):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)

E/AndroidRuntime( 2426):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)

E/AndroidRuntime( 2426):    at android.view.View.measure(View.java:15518)

E/AndroidRuntime( 2426):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)

E/AndroidRuntime( 2426):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)

E/AndroidRuntime( 2426):    at android.view.View.measure(View.java:15518)

E/AndroidRuntime( 2426):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)

E/AndroidRuntime( 2426):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)

E/AndroidRuntime( 2426):    at android.view.View.measure(View.java:15518)

E/AndroidRuntime( 2426):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)

E/AndroidRuntime( 2426):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)

E/AndroidRuntime( 2426):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)

E/AndroidRuntime( 2426):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)

E/AndroidRuntime( 2426):    at android.view.View.measure(View.java:15518)

E/AndroidRuntime( 2426):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)

E/AndroidRuntime( 2426):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)

E/AndroidRuntime( 2426):    at android.view.View.measure(View.java:15518)

E/AndroidRuntime( 2426):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)

E/AndroidRuntime( 2426):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)

E/AndroidRuntime( 2426):    at android.view.View.measure(View.java:15518)

E/AndroidRuntime( 2426):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)

E/AndroidRuntime( 2426):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)

E/AndroidRuntime( 2426):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)

E/AndroidRuntime( 2426):    at android.view.View.measure(View.java:15518)

E/AndroidRuntime( 2426):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)

E/AndroidRuntime( 2426):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)

E/AndroidRuntime( 2426):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)

E/AndroidRuntime( 2426):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)

E/AndroidRuntime( 2426):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)

E/AndroidRuntime( 2426):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)

E/AndroidRuntime( 2426):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)

E/AndroidRuntime( 2426):    at android.view.Choreographer.doFrame(Choreographer.java:532)

E/AndroidRuntime( 2426):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)

E/AndroidRuntime( 2426):    at android.os.Handler.handleCallback(Handler.java:725)

E/AndroidRuntime( 2426):    at android.os.Handler.dispatchMessage(Handler.java:92)

E/AndroidRuntime( 2426):    at android.os.Looper.loop(Looper.java:137)

E/AndroidRuntime( 2426):    at android.app.ActivityThread.main(ActivityThread.java:5041)

E/AndroidRuntime( 2426):    at java.lang.reflect.Method.invokeNative(Native Method)

E/AndroidRuntime( 2426):    at java.lang.reflect.Method.invoke(Method.java:511)

E/AndroidRuntime( 2426):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

E/AndroidRuntime( 2426):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

E/AndroidRuntime( 2426):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

问题在于你的代码

if(row != null)
{
    ...
} else
{
    holder = (StudenttemHolder) row.getTag();
    // At that point row is null.
}