使用SeekBar时崩溃

时间:2014-07-14 14:26:25

标签: android android-fragments android-seekbar

所以我正在开发一款需要SeekBar的应用。此SeekBar位于片段内,并在用户单击下拉按钮时显示。 SeekBar出现并且工作正常。但是,一旦我尝试使用此搜索栏,我就会崩溃。我想要什么来获得条形图的进度,并根据所述条形图的进度显示文本。但是我在发布时遇到了这个崩溃

07-14 10:18:47.650: D/AndroidRuntime(924): Shutting down VM
07-14 10:18:47.650: W/dalvikvm(924): threadid=1: thread exiting with uncaught exception (group=0xb3a47b90)
07-14 10:18:47.670: E/AndroidRuntime(924): FATAL EXCEPTION: main
07-14 10:18:47.670: E/AndroidRuntime(924): Process: com.example.tipquiz, PID: 924
07-14 10:18:47.670: E/AndroidRuntime(924): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tipquiz/com.example.tipquiz.MainActivity}: java.lang.NullPointerException
07-14 10:18:47.670: E/AndroidRuntime(924):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
07-14 10:18:47.670: E/AndroidRuntime(924):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
07-14 10:18:47.670: E/AndroidRuntime(924):  at android.app.ActivityThread.access$700(ActivityThread.java:135)
07-14 10:18:47.670: E/AndroidRuntime(924):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
07-14 10:18:47.670: E/AndroidRuntime(924):  at android.os.Handler.dispatchMessage(Handler.java:102)
07-14 10:18:47.670: E/AndroidRuntime(924):  at android.os.Looper.loop(Looper.java:137)
07-14 10:18:47.670: E/AndroidRuntime(924):  at android.app.ActivityThread.main(ActivityThread.java:4998)
07-14 10:18:47.670: E/AndroidRuntime(924):  at java.lang.reflect.Method.invokeNative(Native Method)
07-14 10:18:47.670: E/AndroidRuntime(924):  at java.lang.reflect.Method.invoke(Method.java:515)
07-14 10:18:47.670: E/AndroidRuntime(924):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
07-14 10:18:47.670: E/AndroidRuntime(924):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
07-14 10:18:47.670: E/AndroidRuntime(924):  at dalvik.system.NativeStart.main(Native Method)
07-14 10:18:47.670: E/AndroidRuntime(924): Caused by: java.lang.NullPointerException
07-14 10:18:47.670: E/AndroidRuntime(924):  at com.example.tipquiz.MainActivity.onCreate(MainActivity.java:36)
07-14 10:18:47.670: E/AndroidRuntime(924):  at android.app.Activity.performCreate(Activity.java:5243)
07-14 10:18:47.670: E/AndroidRuntime(924):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-14 10:18:47.670: E/AndroidRuntime(924):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
07-14 10:18:47.670: E/AndroidRuntime(924):  ... 11 more

现在我怀疑它崩溃的原因是因为我在我的主类中运行所有这些代码,而不是片段类,但当我尝试将代码移到片段类时,我遇到了错误findViewById行。

这是我的代码:
我的主要课程

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.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnRatingBarChangeListener, OnSeekBarChangeListener {

    // Testing Stuff to show the rating value, will need to use later for maths
    RatingBar rb;
    TextView tv;

    SeekBar fqBar;
    TextView fqTV;
    // 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);
        fqBar = (SeekBar)findViewById(R.id.seekBarFQ);
        fqBar.setOnSeekBarChangeListener(this);

        fqTV = (TextView)findViewById(R.id.textViewFQ2);
    }

    @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
    // http://custom-android-dn.blogspot.ca/2013/01/how-to-use-and-custom-ratingbar-in.html

    @Override
    public void onProgressChanged(SeekBar arg0, int progress, boolean fromUser) {
        if(progress > 70 && progress < 90){
            fqTV.setText("Above Average");
        }else if(progress > 40 && progress <= 70){
            fqTV.setText("Average");
        }else if(progress <= 40){
            fqTV.setText("Awful");
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub

    }
}

主要XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#bbcde3"
    android:orientation="vertical" >

    <GridLayout
        android:id="@+id/gridLayout1"
        android:layout_width="fill_parent"
        android:layout_height="35dip"
        android:background="#e3e3e3"
        android:columnCount="2"
        android:gravity="right"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="108dp"
            android:layout_height="match_parent"
            android:layout_column="1"
            android:layout_gravity="right|top"
            android:layout_row="0"
            android:background="#3fa9f5"
            android:fontFamily="helvetica"
            android:text="@string/settings_button"
            android:textColor="#ffffff"
            android:textSize="14sp"
            android:textStyle="bold" />
    </GridLayout>

    <Space
        android:layout_width="match_parent"
        android:layout_height="12sp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/gridLayout1"
        android:layout_marginTop="24dp"
        android:text="@string/rys"
        android:textColor="#888888"
        android:textSize="19sp" />

    <RatingBar
        android:id="@+id/ratingBar1"
        style="@style/circleRatingBar"
        android:layout_width="wrap_content"
        android:layout_height="47dp"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:numStars="5"
        android:rating="3.0"
        android:stepSize="1.0" />

    <ImageView
        android:id="@+id/dropDownButton"
        android:layout_width="48dip"
        android:layout_height="48dip"
        android:layout_alignBottom="@+id/ratingBar1"
        android:layout_toRightOf="@+id/ratingBar1"
        android:onClick="dropDown"
        android:src="@drawable/ddb" />

    <RelativeLayout
        android:id="@+id/dropDownLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/textView2"
        android:visibility="gone" >

        <TextView
            android:id="@+id/testTV"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Testing dropdown" />
    </RelativeLayout>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/ratingBar1"
        android:layout_marginLeft="14dp"
        android:layout_marginTop="16dp"
        android:text="@string/tipTitle"
        android:textColor="#888888"
        android:textSize="19sp" />

    <FrameLayout
        android:id="@+id/quizFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/dropDownButton" />


</RelativeLayout>

片段类和XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewFQ"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_marginLeft="12dp"
        android:text="@string/food_quality"
        android:textColor="#888888"
        android:textSize="19sp" />

    <TextView
        android:id="@+id/textViewFQ2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textViewFQ"
        android:layout_marginTop="24dp"
        android:layout_marginLeft="5dp"
        android:textColor="#888888"
        android:textSize="19sp" />

    <SeekBar
        android:id="@+id/seekBarFQ"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textViewFQ"
        android:layout_marginLeft="12sp"
        android:layout_marginRight="12sp"
        android:layout_marginTop="24dp"
        android:background="#f0f0f0"
        android:progressDrawable="@xml/progress_drawable"
        android:thumb="@xml/thumb_drawable"
        android:max="100"
        android:progress="15" />


</RelativeLayout>



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.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class QuizFragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.quiz_fragment_layout, container, false);
    }

}

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

你没有获得搜索栏,因为它是片段xml。

您需要在onCreateView中找到它:

View view = inflater.inflate(R.layout.quiz_fragment_layout, container, false);
fqBar = (SeekBar) view.findViewById(R.id.seekBarFQ);

希望它有所帮助。

答案 1 :(得分:-1)

问题在于这行代码

tv.setText(评级+“/ 5.0”);

在onRatingChanged方法中,你刚刚声明了TextView tv但从未初始化它。