2014-07-14 7 views
-1

그래서 SeekBar가 필요한 앱을 만들고 있습니다. 이 SeekBar는 Fragment 안에 있으며 사용자가 드롭 다운 버튼을 클릭 할 때 나타납니다. 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); 
    } 

} 
여기

내 코드입니다

도움이 될 것입니다.

+1

'원인 : java.lang.NullPointerException' - null이 무엇인지 찾아서 그렇지 않은지 확인하십시오. – 323go

답변

1

조각 모음 xml로 인해 seekbar가 표시되지 않습니다.

이 같은 onCreateView에서 찾을해야합니다

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

는 도움이되기를 바랍니다.

+0

이것은 Fragment 클래스에 들어 있습니까? –

+0

예. 참조를 위해 [this] (http://stackoverflow.com/a/24508799/1777090)를보십시오. –

+0

우수 감사합니다! –

-1

문제는이 코드 줄

tv.setText (평가 + "/5.0")에;

onRatingChanged 메서드 내부에 TextView tv가 선언되었지만 초기화되지 않았습니다.

관련 문제