所以我写的应用程序有两个(到目前为止)处于片段中的搜索栏。评级栏位于主布局文件中。我需要做的是评级栏根据SeekBars的值进行更改。这是代码,所以你有一个更好的主意:
片段Java文件
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
}
}
主要类
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
}
我尝试过多种方式,而且我做过这种方式的大部分方式都让我在Eclipse中出错,或者崩溃。我现在使用的方法不会崩溃或出现任何错误,但评级栏在预期时不会发生变化。
我还试图简单地使用MainActivity.rb.setProgress(4);在if语句中,但我遇到了这个崩溃:
D /(938):HostConnection :: get()建立新主机连接0xb7ca38d0,tid 938 W / EGL_emulation(938):eglSurfaceAttrib未实现 D / OpenGLRenderer(938):启用调试模式0 E / InputEventReceiver(938):异常调度输入事件。 关闭VM W / dalvikvm(938):threadid = 1:线程退出,未捕获异常(group = 0xb3b0db90) D / dalvikvm(938):GC_FOR_ALLOC释放94K,6%自由3184K / 3360K,暂停31ms,总计37ms E / AndroidRuntime(938):致命异常:主要 E / AndroidRuntime(938):处理: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):在android.widget.SeekBar.onProgressRefresh(SeekBar.java:91) E / AndroidRuntime(938):在android.widget.ProgressBar.doRefreshProgress(ProgressBar.java:655) E / AndroidRuntime(938):在android.widget.ProgressBar.refreshProgress(ProgressBar.java:667) E / AndroidRuntime(938):在android.widget.ProgressBar.setProgress(ProgressBar.java:714) E / AndroidRuntime(938):在android.widget.AbsSeekBar.trackTouchEvent(AbsSeekBar.java:451) E / AndroidRuntime(938):在android.widget.AbsSeekBar.onTouchEvent(AbsSeekBar.java:379) E / AndroidRuntime(938):在android.view.View.dispatchTouchEvent(View.java:7690) E / AndroidRuntime(938):在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) E / AndroidRuntime(938):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959) E / AndroidRuntime(938):在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) E / AndroidRuntime(938):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959) E / AndroidRuntime(938):在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) E / AndroidRuntime(938):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959) E / AndroidRuntime(938):在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) E / AndroidRuntime(938):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959) E / AndroidRuntime(938):在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) E / AndroidRuntime(938):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959) E / AndroidRuntime(938):在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216) E / AndroidRuntime(938):在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):在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):在android.view.View.dispatchPointerEvent(View.java:7870) E / AndroidRuntime(938):在android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3919) E / AndroidRuntime(938):在android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:3808) E / AndroidRuntime(938):在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3406) E / AndroidRuntime(938):在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3456) E / AndroidRuntime(938):在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3425) E / AndroidRuntime(938):在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:3510) E / AndroidRuntime(938):在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3433) E / AndroidRuntime(938):在android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:3567) E / AndroidRuntime(938):在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3406) E / AndroidRuntime(938):在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3456) E / AndroidRuntime(938):在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3425) E / AndroidRuntime(938):在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3433) E / AndroidRuntime(938):在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3406) E / AndroidRuntime(938):在android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5520) E / AndroidRuntime(938):在android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5500) E / AndroidRuntime(938):在android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5471) E / AndroidRuntime(938):在android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5594) E / AndroidRuntime(938):在android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182) E / AndroidRuntime(938):在android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method) E / AndroidRuntime(938):在android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:174) E / AndroidRuntime(938):在android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:5573) E / AndroidRuntime(938):在android.view.ViewRootImpl $ ConsumeBatchedInputRunnable.run(ViewRootImpl.java:5613) E / AndroidRuntime(938):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:749) E / AndroidRuntime(938):在android.view.Choreographer.doCallbacks(Choreographer.java:562) E / AndroidRuntime(938):在android.view.Choreographer.doFrame(Choreographer.java:530) E / AndroidRuntime(938):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:735) E / AndroidRuntime(938):在android.os.Handler.handleCallback(Handler.java:733) E / AndroidRuntime(938):在android.os.Handler.dispatchMessage(Handler.java:95) E / AndroidRuntime(938):a
答案 0 :(得分:1)
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);
}
在 QuizFragment 中: 删除此行
rb = (RatingBar) mainView.findViewById(R.id.ratingBar1);
然后:
if (progress > 70 && progress < 90){
fqTV.setText("Above Average - ");
MainActivity.rating = 4;
}
答案 1 :(得分:0)
尝试设置浮点值。而不是4尝试把4f