为什么我在setAdapter中为listview获取Nullpointerexception?

时间:2015-11-24 12:55:35

标签: android listview nullpointerexception android-adapter

我一直得到以下NullPointerException,我无法理解任何帮助将不胜感激。我正在使用片段

这是我在logcat中得到的:

11-24 13:48:11.130 24367-24367/com.example.hadi.do2get E/AndroidRuntime: FATAL EXCEPTION: main
11-24 13:48:11.130 24367-24367/com.example.hadi.do2get E/AndroidRuntime: Process: com.example.hadi.do2get, PID: 24367
11-24 13:48:11.130 24367-24367/com.example.hadi.do2get E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
11-24 13:48:11.130 24367-24367/com.example.hadi.do2get E/AndroidRuntime:     at com.example.hadi.do2get.Rate_frag.onCreateView(Rate_frag.java:27)
11-24 13:48:11.130 24367-24367/com.example.hadi.do2get E/AndroidRuntime:     at android.app.Fragment.performCreateView(Fragment.java:2053)
11-24 13:48:11.130 24367-24367/com.example.hadi.do2get E/AndroidRuntime:     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
11-24 13:48:11.130 24367-24367/com.example.hadi.do2get E/AndroidRuntime:     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
11-24 13:48:11.130 24367-24367/com.example.hadi.do2get E/AndroidRuntime:     at android.app.BackStackRecord.run(BackStackRecord.java:834)
11-24 13:48:11.130 24367-24367/com.example.hadi.do2get E/AndroidRuntime:     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
11-24 13:48:11.130 24367-24367/com.example.hadi.do2get E/AndroidRuntime:     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
11-24 13:48:11.130 24367-24367/com.example.hadi.do2get E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
11-24 13:48:11.130 24367-24367/com.example.hadi.do2get E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
11-24 13:48:11.130 24367-24367/com.example.hadi.do2get E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
11-24 13:48:11.130 24367-24367/com.example.hadi.do2get E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5257)
11-24 13:48:11.130 24367-24367/com.example.hadi.do2get E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-24 13:48:11.130 24367-24367/com.example.hadi.do2get E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
11-24 13:48:11.130 24367-24367/com.example.hadi.do2get E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
11-24 13:48:11.130 24367-24367/com.example.hadi.do2get E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

RateAdapter:

public class RateAdapter extends ArrayAdapter<Annonce> {

TextView txtTitle;
ImageView imageView;
TextView extratxt;
RatingBar ratingBar;

private final Activity context;
LayoutInflater inflater;

public RateAdapter(Activity context, Annonce[] annoncer) {
    super(context, R.layout.rate, annoncer);
    this.context=context;
}

public View getView(int position,View view,ViewGroup parent) {

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

        view = inflater.inflate(R.layout.rate, null);


    txtTitle = (TextView) view.findViewById(R.id.antitle);
    imageView = (ImageView) view.findViewById(R.id.animage);
    extratxt = (TextView) view.findViewById(R.id.andescrip);
    ratingBar=(RatingBar) view.findViewById(R.id.ratingBar);

    Annonce currentItem = getItem(position);

    txtTitle.setText(currentItem.getItemname());
    imageView.setImageResource(currentItem.getImgid());
    extratxt.setText(currentItem.getDescription());
    ratingBar.setNumStars(5);


    return view;

 }

}

这是Rate_Frag:

public class Rate_frag extends Fragment implements Liste {

ListView ratelist;
RatingBar ratebar;
RateAdapter rateAdapter;


public View onCreateView(LayoutInflater i, ViewGroup container, Bundle savedInstanceState) {
    View rod = i.inflate(R.layout.rate, container, false);

    ratelist = (ListView) rod.findViewById(R.id.ratelistview);
    rateAdapter = new RateAdapter (getActivity(),ANNONCER); 

    ratelist.setAdapter(rateAdapter); //this is Rate_frag:27

    return rod;
 }


}

我的费率xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="{relativePackage}.${activityClass}">


<ImageView
    android:id="@+id/animage"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginLeft="15dp"
    android:padding="10dp"
    android:src="@drawable/sushi"
    android:layout_below="@+id/antitle"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/antitle"
    android:layout_alignParentTop="true"
    android:layout_alignStart="@+id/animage"
    android:layout_marginTop="31dp"
    android:textSize="20dp"
    android:text="title"
    android:layout_alignEnd="@+id/andescrip" />

<TextView
    android:layout_width="168dp"
    android:layout_height="wrap_content"
    android:text="werqwjrlqewjrlqwrjwqeljrwljlqjkrlkewjqljkrlkqewjlkrjlqkewjkrqjlwrjq"
    android:id="@+id/andescrip"
    android:layout_alignTop="@+id/animage"
    android:layout_toEndOf="@+id/animage" />

<RatingBar

    android:layout_width="78dp"
    android:layout_height="wrap_content"
    android:id="@+id/ratingBar"
    style="?android:attr/ratingBarStyleSmall"
    android:numStars="5"
    android:rating="5"
    android:scaleX="0.5"
    android:scaleY="0.5"
    android:layout_alignTop="@+id/antitle"
    android:layout_toEndOf="@+id/andescrip" />

这是我的费率列表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">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ratelistview"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

0 个答案:

没有答案