gpt4 book ai didi

java - 我在 Android 中遇到了 null 异常,我找不到位置,最糟糕的是 - 为什么?

转载 作者:行者123 更新时间:2023-12-01 23:12:59 26 4
gpt4 key购买 nike

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)

最佳答案

最佳猜测...这两行:

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);

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

关于java - 我在 Android 中遇到了 null 异常,我找不到位置,最糟糕的是 - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21589808/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com