我在Android中有一个null异常,我无法找到哪里,最糟糕的是 - 为什么?

时间:2014-02-05 22:19:46

标签: java android eclipse android-layout listview

public class ChooseFavorites extends Activity implements OnItemClickListener
{
    String[] stations;
    float[] rating;
    RatingAdapter ratingAdapter; 
    ArrayAdapter<String> arr;
    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_favorites);
        stations =  getResources().getStringArray(R.array.stations);
        rating = new float[stations.length];
        ratingAdapter = new RatingAdapter(this, R.layout.favorite_row, stations);
        list = (ListView)findViewById(R.id.stationsList1);
        list.setAdapter(ratingAdapter);
        list.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

    }




    class RatingAdapter extends ArrayAdapter<String> 
    {
        Context ctx;
        LayoutInflater inflater;
        TextView stationName;
        RatingBar star;

        public RatingAdapter(Context context, int textViewResourceId, String[] objects) 
        {
            super(context, textViewResourceId, objects);
            ctx = context;
            inflater = LayoutInflater.from(context);
        }



@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    View row = convertView;
    if(row==null)
    { // Object reuse
        inflater = getLayoutInflater();
        row = inflater.inflate(R.layout.favorite_row, parent, false);
    }
    stationName = (TextView)findViewById(R.id.textFavoriteItemInRow);
    stationName.setText(stations[position]);
    star= (RatingBar)findViewById(R.id.ratingBar1);
    star.setRating(rating[position]);
    star.setTag(position);
    star.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener()
    {
        public void onRatingChanged(RatingBar ratingBar, float rating2, boolean fromUser)
        {
            if(!fromUser) return;
            int index = (Integer)(ratingBar.getTag());
            rating[index] = rating2;
        }
    });
    return row;
}


    }

}

我有这个错误:

02-06 00:17:34.052: E/AndroidRuntime(31247): FATAL EXCEPTION: main
02-06 00:17:34.052: E/AndroidRuntime(31247): java.lang.NullPointerException
02-06 00:17:34.052: E/AndroidRuntime(31247):    at com.example.finalprojecttrainsleep.ChooseFavorites$RatingAdapter.getView(ChooseFavorites.java:85)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.widget.AbsListView.obtainView(AbsListView.java:2465)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.widget.ListView.onMeasure(ListView.java:1162)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.view.View.measure(View.java:15288)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.view.View.measure(View.java:15288)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.view.View.measure(View.java:15288)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.view.View.measure(View.java:15288)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4876)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2397)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.view.View.measure(View.java:15288)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1974)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1217)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1390)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1113)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4481)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.view.Choreographer.doFrame(Choreographer.java:525)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.os.Handler.handleCallback(Handler.java:615)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.os.Looper.loop(Looper.java:137)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at android.app.ActivityThread.main(ActivityThread.java:4856)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at java.lang.reflect.Method.invokeNative(Native Method)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at java.lang.reflect.Method.invoke(Method.java:511)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
02-06 00:17:34.052: E/AndroidRuntime(31247):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:3)

最佳猜测......这两行:

stationName = (TextView)findViewById(R.id.textFavoriteItemInRow);
star= (RatingBar)findViewById(R.id.ratingBar1);

应如下所示:

stationName = (TextView)row.findViewById(R.id.textFavoriteItemInRow);
star= (RatingBar)row.findViewById(R.id.ratingBar1);

否则谁知道你实际引用的是什么......