2017-01-26 8 views
0

私はアンドロイドアプリを作っています。私はそれを押しても反応しないボタンがあります。ボタンは警告ダイアログボックスを表示するはずです。私はOnClickListenerを持っているので、ダイアログボックスがインスタンス化されるので、私は非常に混乱しています。フラグメントからのボタンの検索方法

public class tab1Expenses extends Fragment { 

    Button btnEx; 

    public class button extends AppCompatActivity { 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.tab1expense); 
      btnEx = (Button)findViewById(R.id.btnEx); 

      btnEx.setOnClickListener(new View.OnClickListener(){ 

       @Override 
       public void onClick(View v) { 
        View view = LayoutInflater.from(button.this).inflate(R.layout.add_ex, null); 

        AlertDialog.Builder add = new AlertDialog.Builder(button.this); 
        add.setView(view); 

        add.setCancelable(true) 
         .setTitle("Enter Expense:") 
         .setMessage("Expense Name:") 
         .setPositiveButton("Add", new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, int which) { 

          } 
         }); 

        Dialog dialog = add.create(); 
        dialog.show(); 
       } 
      }); 
     } 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.tab1expense, container, false); 
     return rootView; 
    } 

} 

そして、ここでのjavaファイルに関連している私のXMLファイルである:ここで

は、Javaファイルからコードがある

<RelativeLayout 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" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.ojemz.expensetracker.tab1Expenses$button" 
android:background="@android:color/darker_gray"> 

<Button 
    android:text="Add Expense" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/btnEx" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:width="800dp" 
    android:textAppearance="@style/TextAppearance.AppCompat.Widget.Switch" 
    android:background="@android:color/black" 
    android:textColor="@android:color/holo_green_light" /> 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginBottom="17dp"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <!-- TextView and other stuff --> 

    </LinearLayout> 
</ScrollView> 

+0

なぜフラグメント内にアクティビティがありますか?それは私が置くLayoutInflater宣言とAlerdialog宣言以外の点では間違っていると思われる –

答えて

1

あなたは」それは間違っている!

button拡張クラスActivityは、ここに存在する理由はありません。

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.tab1expense, container, false); 
    btnEx = (Button)rootView.findViewById(R.id.btnEx); 
    btnEx.setOnClickListener(new View.OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       View view = LayoutInflater.from(button.this).inflate(R.layout.add_ex, null); 
       AlertDialog.Builder add = new AlertDialog.Builder(tab1Expenses.this.getActivity()); 
       add.setView(view); 
       add.setCancelable(true) 
        .setTitle("Enter Expense:") 
        .setMessage("Expense Name:") 
        .setPositiveButton("Add", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
         } 
        }); 
       Dialog dialog = add.create(); 
       dialog.show(); 
      } 
     }); 
    return rootView; 
} 

あなたの「ボタン」内部クラスを削除することを忘れないでください:

あなたは、単にいくつかのonClickコールバックを取得したい場合は

だけで次の操作を行います。

サイドノート

あなたは、インスタンスとクラスを区別するために、Tab1Expensesにごtab1ExpensesFragmentクラスの名前を変更する必要があります。

Java naming conversionsを参照してください。

+0

です。 'View view = LayoutInflater.from(tab1Expenses.this).inflate(R.layout.add_ex、null); AlertDialog.Builder add =新しいAlertDialog.Builder(tab1Expenses.this); ' 、それはそれはrootview.getContextで – Corpse

+0

ビルダー (android.content.Context)Builderで が へ(com.example.ojemz.expensetracker.tab1Expenses)を適用することはできません – Corpse

+0

私は置き換えtab1Expenses.thisを適用することカント言います()とそれが働いた!ありがとうございます – Corpse

関連する問題