2017-12-18 27 views
-2

:바닥 탐색보기 조각 NullPointerException이 내 조각 중 하나가 내가 XML에서 토글 버튼이 내에서

<FrameLayout 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" 
    tools:context="com.stefan.findage.piano"> 

    <!-- TODO: Update blank fragment layout --> 

    <ToggleButton 
     android:id="@+id/metronome" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="ToggleButton" /> 

</FrameLayout> 

그리고

toggleButton = (ToggleButton) getView().findViewById(R.id.metronome); 

그러나 충돌 조각과 로그 캣을 열려고 할 때 그 것이었다는 것을 보여줍니다.

java.lang.NullPointerException : 가상 메소드 'android.view.View android.view.View.View.Vind null 객체 참조

에 ewById (INT)가 '여기에 주요 활동입니다 :

public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottomView); 
     bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { 
      @Override 
      public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
       switch (item.getItemId()) 
       { 
        case R.id.moosic: 
         rhythm fragmentone = new rhythm(); 
         android.support.v4.app.FragmentTransaction fragmentTransaction3 = getSupportFragmentManager().beginTransaction(); 
         fragmentTransaction3.replace(R.id.frame, fragmentone, "rhythm"); 
         fragmentTransaction3.commit(); 
         return true; 
        case R.id.call: 
         intervals fragmenttwo = new intervals(); 
         android.support.v4.app.FragmentTransaction fragmentTransaction2 = getSupportFragmentManager().beginTransaction(); 
         fragmentTransaction2.replace(R.id.frame, fragmenttwo, "intervals"); 
         fragmentTransaction2.commit(); 
         return true; 
        case R.id.thinga: 
         piano fragmentthree = new piano(); 
         android.support.v4.app.FragmentTransaction fragmentTransaction1 = getSupportFragmentManager().beginTransaction(); 
         fragmentTransaction1.replace(R.id.frame, fragmentthree, "piano"); 
         fragmentTransaction1.commit(); 
         return true; 

       } 
       return false; 
      } 
     }); 

    } 

} 
+0

logcat에서 오류 메시지를 추가하십시오. – Barns

+0

[NullPointerException이란 무엇이며 어떻게 수정합니까?] (https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it)) – Zoe

+0

액티비티에서 프래그먼트가 생성되면 [getView가 null을 반환 할 수 있습니다] 가능한 복제본 (https://stackoverflow.com/questions/9488595/getview-returning-null-when-fragment-has-been-created-from-an -activity) –

답변

0

의 getView() null이 반환 될 수 있습니다. 아래 코드를 사용하여 토글 버튼을 참조하십시오.

View view = inflater.inflate(R.layout.fragmentlayout, container, false); 
toggleButton = (ToggleButton) view.findViewById(R.id.metronome);