2017-11-14 7 views
-3

새 뷰를 열려면 리스너를 사용하여 부동 액션 버튼을 만들려고합니다.null 객체 참조에서 가상 메소드 'void android.widget.TextView.setText (java.lang.CharSequence)'를 호출하려고 시도했습니다. fab

public class ConnectionMenu extends AppCompatActivity { 
ConnectionManager conn = new ConnectionManager(); 
CredentialManager cred = new CredentialManager(); 
FloatingActionButton fab; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_connection_menu); 
    Toolbar toolbar = findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    final EditText field1 = findViewById(R.id.editText); 
    final EditText field2 = findViewById(R.id.editText2); 
    final EditText field3 = findViewById(R.id.editText3); 

    FloatingActionButton fab = findViewById(R.id.fab); 
    Button button = findViewById(R.id.button2); 
    final Spinner spinner = findViewById(R.id.spinner); 
    final Intent intent = new Intent(this, CredentialMenu.class); 
    Object[] credentials = cred.getAllnames(); 

    ArrayAdapter<Object> adapter = new ArrayAdapter<>(this, 
      android.R.layout.simple_spinner_item, R.id.spinner, credentials); 
    spinner.setAdapter(adapter); 
    fab.setOnClickListener(new View.OnClickListener() { //fails here!!!! 
     @Override 
     public void onClick(View view) { 
      startActivity(intent); 
     } 

그러나 제목의 예외는 throw됩니다. 내 XML 파일의 구조는 다음과 같습니다

activity_connection_menu.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="net.ddns.andrewnetwork.raspberrymonitor.View.ConnectionMenu"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 

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

<include layout="@layout/content_connection_menu" /> 

content_connection_menu.xml

... 
     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/fab" 
      android:layout_width="match_parent" 
      android:layout_height="84dp" 
      android:layout_marginLeft="32dp" 
      android:layout_marginStart="32dp" 
      android:layout_weight="1" 
      android:clickable="true" 
      android:focusable="true" 
      app:backgroundTint="@color/colorPrimaryDark" 
      app:srcCompat="@android:drawable/ic_menu_add" /> 

    </LinearLayout> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginEnd="40dp" 
     android:layout_marginStart="40dp" 
     android:layout_marginTop="40dp" 
     android:layout_weight="1" 
     android:text="@string/button1" 
     app:layout_constraintEnd_toEndOf="parent" 
     tools:layout_editor_absoluteY="206dp" /> 

</LinearLayout> 

변수가 있음을 것 같다 즉석 내용은 실제로는 null입니다. 어떤 아이디어?

+0

trye'FloatingActionButton 팹 = (초기화 FloatingActionButton) findViewById (R.id.fab); ' – Ibrahim

+0

주석이 나타내는 곳에서는 예외가 발생하지 않습니다. –

+0

내가 디버깅을하고 그 중단 점 이후에 실패했습니다. – Andrew

답변

0

전체 오류를 올리십시오. 아니면 초기화하지만 (오류의 지시 LINE AT 대부분의 경우) 코드의 어딘가에 사용하지 않은 적어도 하나의 텍스트 뷰가 텍스트보기를 찾아 즉

tv = (TextView) findViewById(R.id.yourID);