2017-04-02 6 views
0

편집기에서 내 코드를 보면 아무 문제가없는 것 같습니다. 그러나 휴대 전화에서 코드를 실행하고 하나의 활동에서 부동 작업 버튼이있는 다른 활동으로 이동하려고하면 부동 동작 버튼은 ID가 Java와 XML에서 정확히 동일 함에도 불구하고 Id로보기를 찾을 수 없습니다.Android 플로팅 작업 버튼이 인식되지 않습니다

내 자바 :

package com.test.app.app2_test1; 

import android.support.design.widget.FloatingActionButton; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 

public class TermsOfUse extends AppCompatActivity { 
    FloatingActionButton mFloatingActionButton; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.terms_of_use); 

     mFloatingActionButton = (FloatingActionButton) mFloatingActionButton.findViewById(R.id.terms_back_fab); 

     mFloatingActionButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       onBackPressed(); 
      } 
     }); 
    } 
} 

내 XML은 : 가상 메서드 '안드로이드를 호출하는

시도 :

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.test.app.app2_test1.TermsOfUse" 
    android:background="@color/backdrop"> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginBottom="44dp" 
     android:orientation="vertical"> 

    </ScrollView> 

    <View 
     android:layout_gravity="bottom" 
     android:layout_width="match_parent" 
     android:layout_height="44dp" 
     android:background="@color/colorPrimary" 
     android:elevation="4dp"/> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/terms_back_fab" 
     android:layout_gravity="bottom" 
     android:layout_width="56dp" 
     android:layout_height="56dp" 
     android:layout_margin="16dp" 
     android:src="@drawable/ic_arrow_back" 
     app:pressedTranslationZ="12dp"/> 

</android.support.design.widget.CoordinatorLayout> 

내 콘솔에서 얻을 메시지는 다음입니다. null 객체 참조에 대한 view.View 및 android.support.design.widget.FloatingActionButton.findViewById (int) '보기

도움을 주시면 감사하겠습니다.

+0

! 나는 계속지나 갔다. 그러나 나는 그것을 잡지 않았다. 새로운 쌍안경이 볼 수있는 놀라운 일입니다. 다시 한번 감사드립니다. – peter

답변

1

뷰를 자체 뷰의 자식으로 취급하고 있습니다!

은 교체 :

mFloatingActionButton = (FloatingActionButton) mFloatingActionButton.findViewById(R.id.terms_back_fab); 

으로 :

mFloatingActionButton = (FloatingActionButton)findViewById(R.id.terms_back_fab); 
안드로이드 스튜디오는 잠재적 인 NPE
+1

감사에 대해 경고해야합니다 – newbRaymond

+0

예! 천만에요 :) – tahsinRupam