2014-03-31 2 views
1

는 동적으로 조각의 테이블에 행을 추가하려합니다. 는 그러나, 나는 몇 가지 런타임 오류가 심각하게 몇 가지 조언을 필요에 직면하고있다.는 조각 중급 (II) : 조각에 Dynmically 추가 행, 안드로이드

로그 캣 :

1) E/AndroidRuntime(1038): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lol/com.example.lol.MainActivity}: android.view.InflateException: Binary XML file line #96: Error inflating class fragment

2) E/AndroidRuntime(1038): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)

3) E/AndroidRuntime(1038):at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2084)

FragmentTwo.Java

import android.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.EditText; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 

public class FragmentTwo extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) 
    { 
     View view = inflater.inflate(R.layout.fragment_two, container, false); 

     TableLayout tl=(TableLayout) getActivity().findViewById(R.id.mainLayout); 


     TableRow tr = new TableRow(getActivity().getApplicationContext()); 
//  tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 


     TextView textview1 = new TextView(getActivity().getApplicationContext()); 
     textview1.setText("unhappy"); 
     tr.addView(textview1); 

     TextView textview2 = new TextView(getActivity().getApplicationContext()); 
     textview2.setText("happy"); 
     tr.addView(textview2); 


     tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 

     return view; 
    } 

MainActivity.java

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    public void selectFrag(View view) { 
     Fragment fr;  
     if(view == findViewById(R.id.button2)) { 
      fr = new FragmentTwo(); 

     }else { 
      fr = new FragmentOne(); 
     } 
     FragmentManager fm = getFragmentManager(); 
     FragmentTransaction fragmentTransaction = fm.beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_place, fr); 
     fragmentTransaction.commit(); 

    } 

} 

fragment_two.xml

<?xml version="1.0" encoding="UTF-8"?> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="#ffff00"> 


     <TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/mainLayout"> 

     <TableRow 
       android:id="@+id/infoRow" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 

       <TextView 
         android:text="Unhappy" 
         android:id="@+id/column1" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content"> 
       </TextView> 

       <TextView 
         android:text="Happy" 
         android:id="@+id/column2" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content"> 
       </TextView> 


     </TableRow> 
</TableLayout> 
</LinearLayout> 
+0

전체 스택 트레이스에게 안내 – Raghunandan

답변

1

변경이이 NullPointerException을 방지 한 실수

TableLayout tl=(TableLayout) getActivity().findViewById(R.id.mainLayout); 

TableLayout tl=(TableLayout) view.findViewById(R.id.mainLayout); 

에.

및 뷰를 초기화 할 getActivity()를 사용합니다.

When to call activity context OR application context?

+0

감사를 게시 trought을 읽어야합니다. 왜 우리는 활동보다 마지막으로 getApplicationContext() 대신 getActivity()를 사용하는지 이해합니다. – lonelearner

+0

조언이 작동하지만 다른 문제가 발생합니다. 이전에는 2 개의 버튼이 있었고, 각 버튼을 클릭하면 사용자가 각 단편에 곧바로 연결됩니다. 이 솔루션을 구현할 때 조각 1 단추가 작동하지 않습니다. 이 질문을 다른 스레드에 게시해야합니까? 친절하게 조언을 구하십시오. – lonelearner

+0

@lonelearner 올바르게 작동하면 제대로 작동합니다. 예 새 peoblem 새로운 질문을 게시하고이 질문을 편집하면 조각에 대한 자세한 내용을 배우는 사람들을 위해 완전히 – Raghunandan

1

을 읽을 이유를 알고 저도 같은 문제가 있었는데, 내가 대신 활동 컨텍스트의 애플리케이션 컨텍스트를 통과 때문이었다.

 TableRow tr = new TableRow(getActivity()); 

     TextView textview1 = new TextView(getActivity()); 
     textview1.setText("unhappy"); 
     tr.addView(textview1); 

     TextView textview2 = new TextView(getActivity()); 
     textview2.setText("happy"); 
     tr.addView(textview2); 

또한이

Android: How to track down the origin of a InflateException?

+1

는 조언을 주셔서 감사합니다 – lonelearner

관련 문제