alertdialogでtextviewを動的に追加しようとしています。しかし、nullポインタエラーで終わる。 私のコード...AndroidのAlertDialogに動的にテキストビューを追加する
att_layout = (LinearLayout)findViewById(R.id.att_layout);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(Consultation.this);
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
dialogBuilder.setView(dialogView);
TextView con = (TextView) dialogView.findViewById(R.id.textView29);
con.setText("Consultation on " + item.getString(4));
TextView des = (TextView) dialogView.findViewById(R.id.textView28);
des.setText(item.getString(2));
TextView pre = (TextView) dialogView.findViewById(R.id.textView31);
pre.setText(item.getString(3));
TextView fee = (TextView) dialogView.findViewById(R.id.textView32);
fee.setText(item.getString(5));
if (att.getCount() == 0) {
att.close();
} else {
//here is the part where error occurs
View addView = inflater.inflate(R.layout.att_values, null);
TextView att_name=(TextView)addView.findViewById(R.id.textView34);
att_name.setText(att.getString(3));
TextView att_value=(TextView)addView.findViewById(R.id.textView26);
att_value.setText(att.getString(4));
att_layout.addView(addView);
}
adapter1 = new GridViewAdapter(FilePathStrings, getApplicationContext());
grid = (GridView) dialogView.findViewById(R.id.gridview);
grid.setAdapter(adapter1);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + FilePathStrings[position]), "image/*");
startActivity(intent);
}
});
final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
alert_label_editor.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="10dp"
android:paddingBottom="20dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView29"
android:layout_weight="1"
android:gravity="center"
android:textSize="24sp"
android:layout_marginBottom="10dp"
android:textStyle="normal|bold" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView27"
android:layout_weight="2" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView28"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Prescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView30"
android:layout_weight="2" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView31"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="fee"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView33"
android:layout_weight="2" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView32"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/att_layout"></LinearLayout>
</LinearLayout>
</ScrollView>
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</LinearLayout>
att_values.xml
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView34"
android:layout_weight="1"/>
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView26"
android:layout_weight="1"/>
私はそれらのDYNAMを追加ICALLY worling ..その停止し、コードを追加する...全体他のコードは
Logcat ...
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference
どのように私はこの問題を解決することができます...正常に動作していますか。?
L 'R.id.att_layout'のようなooksはこの' Activity'のビューの一部ではありません。レイアウトXMLファイルを共有できますか? –
xmlコードが追加されました – sony