在片段内调用List Adapter时发生崩溃

时间:2014-04-16 10:01:07

标签: android android-fragments android-listview android-navigation

我的进口

import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.dwellesque.R;
import com.dwellesque.util.Extraadap;
import com.dwellesque.util.XMLfunctions;

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
 import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class LoginsearchResult extends Fragment{

查看创建方法

public LoginsearchResult()
{

}
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {
         View rootView;
         String data=null;


         rootView =  inflater.inflate(R.layout.loginsearchresult,container,false);
         txtType = (TextView) rootView.findViewById(R.id.logintype);
          lstJobs= (ListView) rootView.findViewById(R.id.loginlist);
         lstJobs.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);

         showdata(data);

         return rootView;

    }

呼叫清单适配器

Extraadap adapter= new Extraadap(getActivity(),strimage, strname, loc, square, price,strposted);
            lstJobs.setAdapter(adapter);

Extraadap class

 public class Extraadap extends ArrayAdapter<String>{
ImageView imgProperty;
TextView txtposted;
TextView txtStreetName;
TextView txtLocation;
TextView txtSquareFotage;
TextView txtPrice;
ImageView imglike;
Context context;
String[] strimgview,strstreetname,strLocation,strSquarefootage,strPrice,strPosted;
 public ImageLoader imageLoader; 
public Extraadap(Context context,String[] strimgview, String[] strstreetname, String[] strLocation,
                            String[] strSquarefootage, String[] strPrice,String[] strPosted)
{
    super(context, R.layout.propertyrow,strstreetname);
    this.context= context;
    this.strimgview= strimgview;
    this.strstreetname= strstreetname;
    this.strLocation= strLocation;
    this.strSquarefootage= strSquarefootage;
    this.strPrice= strPrice;
    this.strPosted= strPosted;

     imageLoader = new ImageLoader(context);
}
 public View getView(int position, View convertView, ViewGroup parent) {
     LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
     View row;
     row=mInflater.inflate(R.layout.propertyrow, parent,false);
     imgProperty =(ImageView) row.findViewById(R.id.npTupleImage);
     txtStreetName= (TextView) row.findViewById(R.id.npTupleProjectName);
     txtLocation= (TextView) row.findViewById(R.id.npTupleBuilder);
     txtSquareFotage= (TextView) row.findViewById(R.id.npTupleProjectAddress);
     txtPrice =(TextView) row.findViewById(R.id.npTupleProjectDistance);
     txtposted =(TextView) row.findViewById(R.id.npTuplePossession);
     imglike= (ImageView) row.findViewById(R.id.unsavebtn);

     txtposted.setText("EnteredDate :"+strPosted[position]);
      txtStreetName.setText("Street Name :"+strstreetname[position]);
       txtLocation.setText("Location :"+strLocation[position]);
      txtSquareFotage.setText("SquareFootage :"+strSquarefootage[position]);
      txtPrice.setText("Price :$"+strPrice[position]);
      Log.d("Img",strimgview[position]);
     try{
      imageLoader.DisplayImage(strimgview[position], imgProperty);
      Log.d("imageUrl", strimgview[position]);
     }
     catch(Exception e)
     {
         Toast.makeText(context, "Load"+e, Toast.LENGTH_LONG).show();
     }
      imglike.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "Want to save", Toast.LENGTH_LONG).show();

        }
    });
     return row;


 }

}

当我调用LoginsearchResult碎片类时,应用程序崩溃

我的LogCat

 04-16 15:03:14.815: E/AndroidRuntime(1956): FATAL EXCEPTION: main
   04-16 15:03:14.815: E/AndroidRuntime(1956):
   java.lang.NullPointerException 04-16 15:03:14.815:
   E/AndroidRuntime(1956):  at
   com.dwellesque.LoginsearchResult.showdata(LoginsearchResult.java:249)
   04-16 15:03:14.815: E/AndroidRuntime(1956):  at
   com.dwellesque.LoginsearchResult.onCreateView(LoginsearchResult.java:57)
   04-16 15:03:14.815: E/AndroidRuntime(1956):  at
   android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
   04-16 15:03:14.815: E/AndroidRuntime(1956):  at
   android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
   04-16 15:03:14.815: E/AndroidRuntime(1956):  at
   android.app.BackStackRecord.run(BackStackRecord.java:635) 04-16
   15:03:14.815: E/AndroidRuntime(1956):    at
   android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
   04-16 15:03:14.815: E/AndroidRuntime(1956):  at
   android.app.FragmentManagerImpl$1.run(FragmentManager.java:426) 04-16
   15:03:14.815: E/AndroidRuntime(1956):    at
   android.os.Handler.handleCallback(Handler.java:615) 04-16
   15:03:14.815: E/AndroidRuntime(1956):    at
   android.os.Handler.dispatchMessage(Handler.java:92) 04-16
   15:03:14.815: E/AndroidRuntime(1956):    at
   android.os.Looper.loop(Looper.java:137) 04-16 15:03:14.815:
   E/AndroidRuntime(1956):  at
   android.app.ActivityThread.main(ActivityThread.java:4745) 04-16
   15:03:14.815: E/AndroidRuntime(1956):    at
   java.lang.reflect.Method.invokeNative(Native Method) 04-16
   15:03:14.815: E/AndroidRuntime(1956):    at
   java.lang.reflect.Method.invoke(Method.java:511) 04-16 15:03:14.815:
   E/AndroidRuntime(1956):  at
   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
   04-16 15:03:14.815: E/AndroidRuntime(1956):  at
   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 04-16
   15:03:14.815: E/AndroidRuntime(1956):    at
   dalvik.system.NativeStart.main(Native Method) 04-16 15:03:16.616:
   E/Trace(1984): error opening trace file: No such file or directory
   (2)

showdata()方法

    void showdata(String data)
    {
         doc = XMLfunctions.XMLfromString(data);
         NodeList nodes = doc.getElementsByTagName("PROPERTY");
            for (int i = 0; i < nodes.getLength(); i++) {
                Element e = (Element) nodes.item(i);

                // Street Name

                if (!("null").equals(XMLfunctions.getValue(e, "street"))) {
                    mainList.add(XMLfunctions.getValue(e, "street"));
                    arliststitname.add(XMLfunctions.getValue(e, "street"));


                } else {
                    mainList.add(" ");
                }

                // Location

                if (!("null").equals(XMLfunctions.getValue(e, "city"))) {
                    if(!("null").equals(XMLfunctions.getValue(e, "state")))
                    {
                        mainList.add(XMLfunctions.getValue(e, "city")+" ,"+XMLfunctions.getValue(e, "state"));
                        arlistlocation.add(XMLfunctions.getValue(e, "city")+" ,"+XMLfunctions.getValue(e, "state"));
                    }
                     mainList.add(XMLfunctions.getValue(e, "city"));
                     arlistlocation.add(XMLfunctions.getValue(e, "city"));


                } else {
                    mainList.add(" ");
                }

                // Square Footage

                if (!("null").equals(XMLfunctions.getValue(e, "SquareFootage"))) {
                    mainList.add(XMLfunctions.getValue(e, "SquareFootage"));
                    arlistsqare.add(XMLfunctions.getValue(e, "SquareFootage"));


                } else {
                    mainList.add(" ");
                }

           // price

                if (!("null").equals(XMLfunctions.getValue(e, "price"))) {
                    mainList.add(XMLfunctions.getValue(e, "price"));
                    arlistprice.add(XMLfunctions.getValue(e, "price"));


                } else {
                    mainList.add(" ");
                }

                // Images

                if (!("null").equals(XMLfunctions.getValue(e, "picture"))) {
                    mainList.add(XMLfunctions.getValue(e, "picture"));
                    arlistimg.add(XMLfunctions.getValue(e, "picture"));
                     Log.d("imageUrlIf", XMLfunctions.getValue(e, "picture"));

                } else {

                     Log.d("imageUrlelse",XMLfunctions.getValue(e, "picture"));
                    mainList.add("");

                }


                // posted on

                if (!("null").equals(XMLfunctions.getValue(e, "EnteredDate"))) {
                    mainList.add(XMLfunctions.getValue(e, "EnteredDate"));
                    arlistposted.add(XMLfunctions.getValue(e, "EnteredDate"));


                } else {


                    mainList.add("");

                }

                if (!("null").equals(XMLfunctions.getValue(e, "description"))) {
                    mainList.add(XMLfunctions.getValue(e, "description"));
                    arlistDesc.add(XMLfunctions.getValue(e, "description"));


                } else {


                    mainList.add("");

                }

                if (!("null").equals(XMLfunctions.getValue(e, "propertystatus"))) {
                    mainList.add(XMLfunctions.getValue(e, "propertystatus"));
                    arlistproperty.add(XMLfunctions.getValue(e, "propertystatus"));


                } else {


                    mainList.add("");

                }


                if (!("null").equals(XMLfunctions.getValue(e, "EnteredDate"))) {
                    mainList.add(XMLfunctions.getValue(e, "EnteredDate"));
                    arlistyear.add(XMLfunctions.getValue(e, "EnteredDate"));


                } else {


                    mainList.add("");

                }

                //URL

                if (!("null").equals(XMLfunctions.getValue(e, "url"))) {
                    mainList.add(XMLfunctions.getValue(e, "url"));
                    arlisturl.add(XMLfunctions.getValue(e, "url"));


                } else {


                    mainList.add("");

                }


            }

             strposted = new String[arlistposted.size()];
             strposted = arlistposted.toArray(strposted);

               strname = new String[arliststitname.size()];
                strname = arliststitname.toArray(strname);

                loc = new String[arlistlocation.size()];
                loc = arlistlocation.toArray(loc);

                square = new String[arlistsqare.size()];
                square = arlistsqare.toArray(square);

                 price = new String[arlistprice.size()];
                price = arlistprice.toArray(price);

                 strimage = new String[arlistimg.size()];
                 strimage = arlistimg.toArray(strimage);

                 strpStatus = new String[arlistproperty.size()];
                 strpStatus = arlistproperty.toArray(strpStatus);


                 strDesc = new String[arlistDesc.size()];
                 strDesc = arlistDesc.toArray(strDesc);

                 strYear = new String[arlistyear.size()];
                 strYear = arlistyear.toArray(strYear);

                 // int currentPosition =lstJobs.getFirstVisiblePosition();
            // List Adapter

            Extraadap adapter= new Extraadap(getActivity(),strimage, strname, loc, square, price,strposted);
            lstJobs.setAdapter(adapter);

                    // Log.d("List",""+e);
                    // Toast.makeText(getActivity(), "ss"+e, Toast.LENGTH_LONG).show();

                //tSelectionFromTop(currentPosition, 0);
    }

Listview布局

<?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"
android:background="@drawable/bg" >
<LinearLayout 
android:id="@+id/top"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
 android:background="#F0F0F0">

 <TextView

    android:layout_marginTop="0dip"
    android:id="@+id/logintype"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:textStyle="bold"

    android:textSize="18sp"
    android:text="TextView" />
 </LinearLayout>

 <ListView
    android:id="@+id/loginlist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/top" >

  </ListView>

</RelativeLayout>

请帮助我如何解决这个问题

提前致谢。

1 个答案:

答案 0 :(得分:1)

首先纠正这个

lstJobs= (ListView) rootView.findViewById(android.R.id.list);

lstJobs= (ListView) rootView.findViewById(R.id.list);

并确保ListView布局中的list loginsearchresult.xml lstJobs= (ListView) rootView.findViewById(R.id.loginlist);

更新:试试这个

{{1}}