SO the app I am writing has two(so far) seekbars that are in a Fragment. The rating bar is in the Main layout file. What I need to happen is the Rating Bar progress to change based on the values of the SeekBars. Here is the code, so you have a better idea:
Fragment Java File
package com.example.tipquiz;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class QuizFragment extends Fragment implements OnSeekBarChangeListener {
SeekBar fqBar, cBar;
TextView fqTV, cTV;
TextView fqTVP, cTVP;
RatingBar rb;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.quiz_fragment_layout, container,
false);
//Inflate the Main layout to access the RatingBar
View mainView = inflater.inflate(R.layout.activity_main, container, false);
//FoodQuality Bar
fqBar = (SeekBar) view.findViewById(R.id.seekBarFQ);
fqBar.setOnSeekBarChangeListener(this);
fqTV = (TextView) view.findViewById(R.id.textViewFQ2);
fqTVP = (TextView) view.findViewById(R.id.textViewFQP);
//Cleanliness Bar
cBar = (SeekBar) view.findViewById(R.id.seekBarC);
cBar.setOnSeekBarChangeListener(this);
cTV = (TextView) view.findViewById(R.id.textViewC2);
cTVP = (TextView) view.findViewById(R.id.textViewCP);
rb = (RatingBar) mainView.findViewById(R.id.ratingBar1);
return view;
}
@Override
public void onProgressChanged(SeekBar arg0, int progress, boolean fromUser) {
switch(arg0.getId()) {
case R.id.seekBarFQ:
if(progress > 70 && progress < 90){
fqTV.setText("Above Average - ");
rb.setRating(4);
}else if(progress > 40 && progress <= 70){
fqTV.setText("Average - ");
}else if(progress <= 40){
fqTV.setText("Awful - ");
}else if(progress > 90){
fqTV.setText("Perfect - ");
}
fqTVP.setText(progress + "%");
break;
case R.id.seekBarC:
if(progress > 70 && progress < 90){
cTV.setText("Above Average - ");
}else if(progress > 40 && progress <= 70){
cTV.setText("Average - ");
}else if(progress <= 40){
cTV.setText("Awful - ");
}else if(progress > 90){
cTV.setText("Perfect - ");
}
cTVP.setText(progress + "%");
break;
}
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
}
Main Class
package com.example.tipquiz;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity implements OnRatingBarChangeListener{
// Testing Stuff to show the rating value, will need to use later for maths
static RatingBar rb;
TextView tv;
// The Image used as the DropDown button, Rotate code below
ImageView dropDownButton;
Boolean hasRotated = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dropDownButton = (ImageView) findViewById(R.id.dropDownButton);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
FragmentManager fm = getFragmentManager();
QuizFragment qf = new QuizFragment();
public void dropDown(View view){
if(hasRotated == false){
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
dropDownButton.setRotation(90);
ft.add(R.id.quizFragment, qf);
ft.show(qf);
ft.commit();
hasRotated = true;
}else if(hasRotated == true){
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(android.R.animator.fade_out, android.R.animator.fade_out);
dropDownButton.setRotation(0);
hasRotated = false;
ft.remove(qf);
ft.commit();
}
}
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromTouch) {
// final int numStars = ratingBar.getNumStars();
tv.setText(rating + "/5.0");
}
// http://www.compiletimeerror.com/2013/08/android-rating-bar-example.html#.U7SZ5fldXm4
}
I've tried to do this many ways, and most ways I've gone about doing this give me an error in Eclipse, or a crash. The method I'm using now doesn't crash or give any errors, yet the rating bar doesn't change when it is supposed to.
I've also tried to simply use MainActivity.rb.setProgress(4); in the if statement, but I got this crash:
D/(938): HostConnection::get() New Host Connection established 0xb7ca38d0, tid 938 W/EGL_emulation(938): eglSurfaceAttrib not implemented D/OpenGLRenderer(938): Enabling debug mode 0 E/InputEventReceiver(938): Exception dispatching input event. Shutting down VM W/dalvikvm(938): threadid=1: thread exiting with uncaught exception (group=0xb3b0db90) D/dalvikvm(938): GC_FOR_ALLOC freed 94K, 6% free 3184K/3360K, paused 31ms, total 37ms E/AndroidRuntime(938): FATAL EXCEPTION: main E/AndroidRuntime(938): Process: com.example.tipquiz, PID: 938 E/AndroidRuntime(938): java.lang.NullPointerException E/AndroidRuntime(938): at com.example.tipquiz.QuizFragment.onProgressChanged(QuizFragment.java:50) E/AndroidRuntime(938): at android.widget.SeekBar.onProgressRefresh(SeekBar.java:91) E/AndroidRuntime(938): at android.widget.ProgressBar.doRefreshProgress(ProgressBar.java:655) E/AndroidRuntime(938): at android.widget.ProgressBar.refreshProgress(ProgressBar.java:667) E/AndroidRuntime(938): at android.widget.ProgressBar.setProgress(ProgressBar.java:714) E/AndroidRuntime(938): at android.widget.AbsSeekBar.trackTouchEvent(AbsSeekBar.java:451) E/AndroidRuntime(938): at android.widget.AbsSeekBar.onTouchEvent(AbsSeekBar.java:379) E/AndroidRuntime(938 ): at android.view.View.dispatchTouchEvent(View.java:7690) E/AndroidRuntime(938): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) E/AndroidRuntime(938): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959) E/AndroidRuntime(938): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) E/AndroidRuntime(938): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959) E/AndroidRuntime(938): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) E/AndroidRuntime(938): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959) E/AndroidRuntime(938): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) E/AndroidRuntime(938): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959) E/AndroidRuntime(938): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) E/AndroidRuntime(938): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:19 59) E/AndroidRuntime(938): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) E/AndroidRuntime(938): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959) E/AndroidRuntime(938): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2066) E/AndroidRuntime(938): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515) E/AndroidRuntime(938): at android.app.Activity.dispatchTouchEvent(Activity.java:2457) E/AndroidRuntime(938): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2014) E/AndroidRuntime(938): at android.view.View.dispatchPointerEvent(View.java:7870) E/AndroidRuntime(938): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3919) E/AndroidRuntime(938): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3808) E/AndroidRuntime(938): at android.view.ViewRo otImpl$InputStage.deliver(ViewRootImpl.java:3406) E/AndroidRuntime(938): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3456) E/AndroidRuntime(938): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3425) E/AndroidRuntime(938): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3510) E/AndroidRuntime(938): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3433) E/AndroidRuntime(938): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3567) E/AndroidRuntime(938): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3406) E/AndroidRuntime(938): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3456) E/AndroidRuntime(938): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3425) E/AndroidRuntime(938): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3433) E/AndroidRuntime(938): at android.view.ViewRootImpl$InputStage.deliver(View RootImpl.java:3406) E/AndroidRuntime(938): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5520) E/AndroidRuntime(938): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5500) E/AndroidRuntime(938): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5471) E/AndroidRuntime(938): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5594) E/AndroidRuntime(938): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182) E/AndroidRuntime(938): at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method) E/AndroidRuntime(938): at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:174) E/AndroidRuntime(938): at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:5573) E/AndroidRuntime(938): at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5613) E/AndroidRuntime(938): at android.view.Choreographe r$CallbackRecord.run(Choreographer.java:749) E/AndroidRuntime(938): at android.view.Choreographer.doCallbacks(Choreographer.java:562) E/AndroidRuntime(938): at android.view.Choreographer.doFrame(Choreographer.java:530) E/AndroidRuntime(938): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) E/AndroidRuntime(938): at android.os.Handler.handleCallback(Handler.java:733) E/AndroidRuntime(938): at android.os.Handler.dispatchMessage(Handler.java:95) E/AndroidRuntime(938): a
In MainActivity :
public static int rating = 0;
private RatingBar rb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
rb = (RatingBar) findViewById(R.id.ratingBar1);
rb.setRating(rating);
}
And in QuizFragment : REMOVE this line
rb = (RatingBar) mainView.findViewById(R.id.ratingBar1);
then:
if (progress > 70 && progress < 90){
fqTV.setText("Above Average - ");
MainActivity.rating = 4;
}
Try put a float value. instead of 4 try put 4f
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.