我正在尝试将评级栏实现为我拥有的片段类。但我总是得到一个零点异常并崩溃我的应用程序。我阅读的教程都展示了如何在Activity类中实现评级栏。我怎样才能在片段类中实现它?
我的班级
package com.f%%%%%%%%;
import java.util.ArrayList;
...................
public class TheaterDetailFragment extends Fragment {
private ViewPager viewtheater;
private ViewPageAdapter adaptertheater;
private Activity activity;
private CommonVariable commonVariable;
private ProgressDialog dialog;
private ArrayList<BaseElement> TheaterDetail;
private ArrayList<BaseElement> theateritems;
private int id;
private String pos;
private RatingBar ratingBar;
private TextView txtRatingValue;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.theaterdetail_fragment,
container, false);
addListenerOnRatingBar();
activity = this.getActivity();
pos= getArguments().getString(BundleKey.THEATERPOSITION.getKey());
commonVariable = (CommonVariable) activity.getApplication();
viewtheater = (ViewPager) view.findViewById(R.id.theaterdetail);
dialog = new ProgressDialog(TheaterDetailFragment.this.getActivity());
dialog.setMessage("Loading Theater Details");
dialog.setCancelable(true);
new BackGround().execute();
return view;
}
public void addListenerOnRatingBar() {
ratingBar = (RatingBar) getView().findViewById(R.id.ratingBar1);
txtRatingValue = (TextView) getView().findViewById(R.id.txtRatingValue);
//if rating value is changed,
//display the current rating value in the result (textview) automatically
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
txtRatingValue.setText(String.valueOf(rating));
}
});
}
public class BackGround extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
//Theater theaterdetail = (Theater) commonVariable.getTheater().get(id);
TheaterDetail = JSONServices.getTheaterDetails(pos);
return null;
}
@Override
// check again
protected void onPostExecute(Void result) {
commonVariable.setTheaterDetail(TheaterDetail);
adaptertheater = new ViewPageAdapter(commonVariable.getTheaterDetail(),
Element.THEATER_DETAIL.getType(), activity);
viewtheater.setAdapter(adaptertheater);
dialog.dismiss();
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
dialog.show();
super.onPreExecute();
}
}
}
我的布局
<?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"
android:id="@+id/filmhall"
android:background="@color/grey" >
<!--
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
-->
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_marginTop="4dp"
android:textStyle="bold"
android:textSize="15dp" />
<RatingBar
android:id="@+id/ratingBar1"
style="?android:attr/ratingBarStyleSmall"
android:isIndicator="false"
android:layout_marginTop="10dp"
android:layout_marginLeft="4dp"
android:numStars="10"
android:rating="2.0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/txtRatingValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:text="Rating :"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
*其他字段已删除!
我提到的教程
http://www.mkyong.com/android/android-rating-bar-example/
1.最初在findviewbyid有一个错误,我使用getview()修复它。
提前致谢!
UPDATE !!
logcat的
12-30 12:01:23.456: E/AndroidRuntime(13134): FATAL EXCEPTION: main
12-30 12:01:23.456: E/AndroidRuntime(13134): java.lang.NullPointerException
12-30 12:01:23.456: E/AndroidRuntime(13134): at com.fortuna.cinemalk.TheaterDetailFragment.addListenerOnRatingBar(TheaterDetailFragment.java:119)
12-30 12:01:23.456: E/AndroidRuntime(13134): at com.fortuna.cinemalk.TheaterDetailFragment.onCreateView(TheaterDetailFragment.java:98)
12-30 12:01:23.456: E/AndroidRuntime(13134): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
12-30 12:01:23.456: E/AndroidRuntime(13134): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
12-30 12:01:23.456: E/AndroidRuntime(13134): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
12-30 12:01:23.456: E/AndroidRuntime(13134): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-30 12:01:23.456: E/AndroidRuntime(13134): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
12-30 12:01:23.456: E/AndroidRuntime(13134): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
12-30 12:01:23.456: E/AndroidRuntime(13134): at android.os.Handler.handleCallback(Handler.java:587)
12-30 12:01:23.456: E/AndroidRuntime(13134): at android.os.Handler.dispatchMessage(Handler.java:92)
12-30 12:01:23.456: E/AndroidRuntime(13134): at android.os.Looper.loop(Looper.java:123)
12-30 12:01:23.456: E/AndroidRuntime(13134): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-30 12:01:23.456: E/AndroidRuntime(13134): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 12:01:23.456: E/AndroidRuntime(13134): at java.lang.reflect.Method.invoke(Method.java:521)
12-30 12:01:23.456: E/AndroidRuntime(13134): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
12-30 12:01:23.456: E/AndroidRuntime(13134): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-30 12:01:23.456: E/AndroidRuntime(13134): at dalvik.system.NativeStart.main(Native Method)
LOGCAT for MD's和REDDY的答案
12-30 12:17:26.023: E/AndroidRuntime(13545): FATAL EXCEPTION: main
12-30 12:17:26.023: E/AndroidRuntime(13545): java.lang.NullPointerException
12-30 12:17:26.023: E/AndroidRuntime(13545): at com.fortuna.cinemalk.TheaterDetailFragment.addListenerOnRatingBar(TheaterDetailFragment.java:126)
12-30 12:17:26.023: E/AndroidRuntime(13545): at com.fortuna.cinemalk.TheaterDetailFragment.onCreateView(TheaterDetailFragment.java:100)
12-30 12:17:26.023: E/AndroidRuntime(13545): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
12-30 12:17:26.023: E/AndroidRuntime(13545): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
答案 0 :(得分:3)
尝试以RootView
作为参数传递给addListenerOnRatingBar(....)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.theaterdetail_fragment,
container, false);
addListenerOnRatingBar(view);
........
}
并像这样实施addListenerOnRatingBar(....)
。
public void addListenerOnRatingBar(View v) {
ratingBar = (RatingBar) v.findViewById(R.id.ratingBar1);
txtRatingValue = (TextView) v.findViewById(R.id.txtRatingValue);
//if rating value is changed,
//display the current rating value in the result (textview) automatically
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
txtRatingValue.setText(String.valueOf(rating));
}
});
}
答案 1 :(得分:0)
更新此代码
ratingBar = (RatingBar) getView().findViewById(R.id.ratingBar1);
txtRatingValue = (TextView) getView().findViewById(R.id.txtRatingValue);
像这样并将其保留在onCreateView()
中或将该视图传递给该方法以供使用。
public void addListenerOnRatingBar(View view) {
ratingBar = (RatingBar) view.findViewById(R.id.ratingBar1);
txtRatingValue = (TextView) view.findViewById(R.id.txtRatingValue);
}
和来自onCreateView()的调用方法,如addListenerOnRatingBar(view);