TableLayoutに複数のButtonを配置するのですが、前のFragmentで入力された値によって、Buttonの配置を変えるために、xmlにTableRowの配置までを書いて、コード上でそのTableRowをfindByIdで取得して、動的にButtonを配置しようと思っていました。
しかし、途中でアプリがクラッシュしてしまうので、AndroidStudioのデバッガで調べた所、
``````Java```
TableRow tableRow;
という行でクラッシュしています。 これだけで分かることがあるのかわかりませんが、この時点で自分はなにか間違ったことをしているんでしょうか? (追記) まだOnClickメソッドは書いてありませんが、レイアウトの部分は完成しているつもりです 前のFragmentから、何年何月かを受け取って、その月のカレンダーになるようにボタンを配置したいんです。 R.id.row0はいらないんで無視してください。 ```Java import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TableRow; import android.widget.TextView; import java.util.Calendar; /** * A simple {@link Fragment} subclass. */ public class FragmentChooseDay extends Fragment implements View.OnClickListener{ private int tableRowId[] = {R.id.row0, R.id.row1, R.id.row2, R.id.row3, R.id.row3, R.id.row4, R.id.row5}; private Button button[]; private TextView textView; private Calendar calendar; private TableRow tableRow; public FragmentChooseDay() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_choose_day, container, false); Bundle bundle = getArguments(); int month = bundle.getInt("month"); int year = bundle.getInt("year"); textView = (TextView)view.findViewById(R.id.textView); textView.setText(year + "年" + month + "月"); calendar = Calendar.getInstance(); calendar.clear(); calendar.set(year, month, 1); int num_day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); button = new Button[num_day]; int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); int num_Row = 1; for(int i = 0; i < num_day; i++){ button[i].setOnClickListener(this); tableRow = (TableRow)view.findViewById(tableRowId[num_Row]); tableRow.addView(button[i],new TableRow.LayoutParams(dayOfWeek-1)); if(dayOfWeek > 7){ dayOfWeek = 1; num_Row++; } } return view; } @Override public void onClick(View v) { } }
xml
1<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context="user.reminder.FragmentChooseDay"> 6 7 <!-- TODO: Update blank fragment layout --> 8 <TextView 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:textSize="12pt" 12 android:id="@+id/textView"/> 13 <TableLayout 14 android:layout_width="match_parent" 15 android:layout_height="match_parent"> 16 <TableRow 17 android:layout_weight="1" 18 android:id="@+id/row0"></TableRow> 19 <TableRow 20 android:layout_weight="2" 21 android:id="@+id/row1"></TableRow> 22 <TableRow 23 android:layout_weight="2" 24 android:id="@+id/row2"></TableRow> 25 <TableRow 26 android:layout_weight="2" 27 android:id="@+id/row3"></TableRow> 28 <TableRow 29 android:layout_weight="2" 30 android:id="@+id/row4"></TableRow> 31 <TableRow 32 android:layout_weight="2" 33 android:id="@+id/row5"></TableRow> 34 35 </TableLayout> 36 37</LinearLayout> 38
回答1件
あなたの回答
tips
プレビュー