2017-01-25 8 views
0

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 추가 전신적으로 worling .. 그 중지 코드를 추가 ... 전체 다른 코드는

로그 캣 ...

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference 

어떻게이 문제를 해결할 수 있습니다 ... 잘 작동한다.?

+0

L 'R.id.att_layout'과 같은 ooks는이 'Activity' 뷰의 일부가 아닙니다. 레이아웃 xml 파일을 공유 할 수 있습니까? –

+0

xml 코드를 추가했습니다. – sony

답변

0

귀하의 alert_label_editor.xml 파일 : 그래서

View dialogView = inflater.inflate(R.layout.alert_label_editor, null); 

현재보기에서 찾아보십시오, 그것은 발견 att_layoutnull

되어 있지 않으며 실제로 dialogView를 레이아웃을 팽창하고, 사용 후

이동이 라인 :

att_layout = (LinearLayout) dialogView.findViewById(R.id.att_layout); 
0

어쩌면 전체 att_layout = (LinearLayout)findViewById(R.id.att_layout);else에 넣을 수 있으므로 att_layout이 null이 아닌지 확인할 수 있습니다.

또한 R.id.att_layout이 레이아웃에 실제로 있는지 확인하십시오.

편집 :

그래서 당신의 R.id.att_layout, 현재 레이아웃에 바로 존재하지 않는? addView-View 에서처럼 LinearLayout을 먼저 팽창시켜야합니다. 이 라인 실행까지 R.id.att_layout있는 LinearLayout이 팽창되지 않은 포함

관련 문제