我的目标是从评级栏中获取值,然后存储并显示值。
我的onClickMethod
中有以下代码 rb = (RatingBar) findViewById(R.id.ratingBar);
result = (TextView) findViewById(R.id.lblResult);
rb.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
}
});
它给了我这行的错误
rb.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener()
logcat的:
11-19 18:12:10.328 8654-8654/tanav.sharma E/AndroidRuntime: FATAL EXCEPTION: main
Process: tanav.sharma, PID: 8654
java.lang.RuntimeException: Unable to start activity ComponentInfo{tanav.sharma/tanav.sharma.TanavActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RatingBar.setOnRatingBarChangeListener(android.widget.RatingBar$OnRatingBarChangeListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7331)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RatingBar.setOnRatingBarChangeListener(android.widget.RatingBar$OnRatingBarChangeListener)' on a null object reference
at tanav.sharma.TanavActivity.onCreate(TanavActivity.java:101)
at android.app.Activity.performCreate(Activity.java:6904)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7331)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
.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"
android:padding="16dp"
tools:context="tanav.sharma.OneFragment">
<LinearLayout
android:id="@+id/name_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="@string/fname"
android:textSize="20dp"
android:textStyle="bold"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="140dp"
android:layout_height="wrap_content"
android:text="@string/sid"
android:textAlignment="textEnd"
android:textSize="20dp"
android:textStyle="bold"
android:layout_centerInParent="true"/>
</LinearLayout>
<LinearLayout
android:padding="16dp"
android:id="@+id/counter_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/name_id">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Counter: "
android:layout_gravity="left|center_vertical" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/counter"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/five_star"
android:padding="10dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/counter_display">
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="0.5"
android:rating="5.0"
/>
</LinearLayout>
<RelativeLayout
android:id="@+id/download_button"
android:layout_below="@+id/five_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/download_image"
android:text="@string/download"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>
以下文件是.java文件(即MainActivity.java)
public class TanavActivity extends AppCompatActivity {
RatingBar rb;
TextView result;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.ic_action_pro,
R.drawable.ic_action_web
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tanav);
toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/**
* This button is responsible for taking the user back
* to the main activity. Preventing the user from pressing
* the back key multiple times.
*/
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager)findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout)findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
int count_image = 0;
int count_web = 0;
@Override
public void onTabSelected(TabLayout.Tab tab) {
int position = tab.getPosition();
if(position == 0){
count_image = count_image + 1;
TextView counter = (TextView) findViewById(R.id.counter);
counter.setText(Integer.toString(count_image));
}else if(position == 1){
//count_web = count_web + 1;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
count_image = count_image + 1;
TextView counter = (TextView) findViewById(R.id.counter);
counter.setText(Integer.toString(count_image));
}
});
rb = (RatingBar) findViewById(R.id.ratingBar);
result = (TextView) findViewById(R.id.lblResult);
rb.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
}
});
}
答案 0 :(得分:0)
一切似乎都是对的。 您的内容视图一定有问题。 确保这是你的xml。使用具有ratingBar id的xml文件名更改它。
setContentView(R.layout.activity_tanav);