2016-12-20 11 views
1
public class Help extends AppCompatActivity { 

    ImageButton floatButton; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_help); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        AlertDialog.Builder helpAlert = new AlertDialog.Builder(this); 
        helpAlert.setMessage("Send emails to - [email protected] - for more help!") 
          .setPositiveButton("Got It!", new DialogInterface.OnClickListener() { 
           @Override 
           public void onClick(DialogInterface dialog, int which) { 
            dialog.dismiss(); 
           } 
          }) 
          .setTitle("Extra Help") 
          .setIcon(R.drawable.ic_info_black_24dp) 
          .create(); 
        helpAlert.show(); 
      } 
     }); 
    } 
} 

私は立ち往生し続けています。浮動動作ボタンも私が望むものを表示することができません。助けてください!私はエラーが発生し続けていると私はなぜ知りませんか?

+0

エラーが発生したときにログトレースを投稿して、XMLファイルも表示してみてください –

+1

あなたはどちらのエラーが発生していますか? – Reena

+0

エラーはAlertDialog.Builder helpAlert = new AlertDialog.Builder(this);で発生しています。それの下に赤い波打つ線がある(これ)。 – Callum

答えて

1

です。匿名クラス内でthisは、Activity(またはAppCompatActivity)を拡張していない匿名クラス(new View.OnClickListener)の参照として機能しているので、上記の行でエラーが表示されます。単純にあなたの上の行を変更し、エラーを削除するには

:私はHelpを見ることができるようにここで

AlertDialog.Builder helpAlert = new AlertDialog.Builder(Help.this); 

ので、これはすべてのエラーを与えることはありませんAppCompatActivityを拡張して、あなたのクラス名です。

+0

おかげで2時間ほど過ごしました何が間違っていたのか理解する。とてもありがたい!! – Callum

0

<android.support.design.widget.AppBarLayout 
    android:id="@+id/app_bar" 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/app_bar_height" 
    android:fitsSystemWindows="true" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/toolbar_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     app:contentScrim="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

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

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

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

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="@dimen/fab_margin" 
    app:layout_anchor="@id/app_bar" 
    app:layout_anchorGravity="bottom|end" 
    app:srcCompat="@android:drawable/ic_dialog_email" /> 

XMLファイルです。

AlertDialog.Builder helpAlert = new AlertDialog.Builder(this); 

匿名クラス内部: は、あなたが書いているので、あなたがエラーを取得しているXMLファイル

+0

ねえ、あなたの質問への答えとして掲示するのではなく、これを含めるようにあなたの質問を編集することができます:Pあなたは1つの場所で多くのことをやっているように見えます。アンカーのものを削除し、あなたのFABで通常のandroid:srcを使用し、何が起こるかを見てください。また、FABにalignBottom = "true"を設定して、どこに行くかを知ることもできます。何かの背後に現れているかもしれません。 また、将来的にあなたのビューを健全にチェックする良い方法は、プログラムでfindViewById(R.id.fab)を探し、その高さと幅が0でないことを確認することです。 –

関連する問題