为片段内的线性布局创建的对象返回null值

时间:2014-07-24 07:11:17

标签: android android-layout android-fragments

我想为片段xml文件中已经存在的线性布局创建对象,xml文件如下文件名是detailfragment.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:id="@+id/detailBaseLayout"
         > </LinearLayout>

我的类文件DetailFragment.java如下,

         public class DetailFragment extends Fragment {

             @Override
             public View onCreateView(LayoutInflater inflater, ViewGroup container,
  Bundle savedInstanceState) {

                 View view = inflater.inflate(R.layout.detail_fragment,container, false);
                 return view;
              }

              public void setText(String item) {

                  Log.w("DetailFragment", "DetailFragment"+item);

                  LinearLayout Blayout = (LinearLayout) getView().findViewById(R.id.detailBaseLayout);
                  LinearLayout layout = new LinearLayout(getActivity()); 
                  layout.setOrientation(LinearLayout.VERTICAL);
                  Log.w("Entering into common code","Entering into common code="+item);
                  TextView tv = new TextView(getActivity());
                  tv.setTextColor(Color.BLACK);
                  tv.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
                  tv.setTextSize(TypedValue.COMPLEX_UNIT_PT,10);
                  tv.setPadding(6, 0, 0, 0);
                  tv.setText(item);
                  layout.addView(tv);
                  if(Blayout == null) {

                      Log.w("Linear Layout is null","Linear Layout is null"+Blayout); 

                  }
                  Blayout.addView(layout);
             }
       }

当运行上面的代码时,我得到线性布局为空(因此一个空指针异常,在DetailFragment.setText上说错误,等等),即使我已正确地给出了id并使用getView()来获取膨胀的片段view.Also,我得到&#34; item&#34;正确的价值没问题。 这是我的MainActivity代码,我在这里调用setText,

       public class MainActivity extends Activity implements ListFragment.OnItemSelectedListener{


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

           }

           // if the wizard generated an onCreateOptionsMenu you can delete
           // it, not needed for this tutorial

           @Override
           public void onQuestionClicked(String item,ArrayList<AnswerOptions> Answers) {
               Log.d("onRssItemSelected", "onRssItemSelected"+link);
               DetailFragment fragment = (DetailFragment) getFragmentManager()
        .findFragmentById(R.id.detail_fragment);
               if (fragment != null && fragment.isInLayout()) {

                   Log.d("fragmentisnlayout", "fragmentisnlayout"+fragment.isInLayout());
                   fragment.setText(item);
                   for(int i=0;i<Answers.size();i++) {

                      Log.d("onQClick", "onQClick"+Answers.get(i).getAnswerText());
                      fragment.setRadioButton(i,Answers.get(i).getAnswerText());

                   }

               } 
               Log.d("onRssItemSelected", "onRssItemSelected"+fragment);
           }

     } 

1 个答案:

答案 0 :(得分:1)

setText调用的地点和时间?必须在getView()中触发该问题。

在onViewCreated中使用setText(View _view,Bundle _savedInstanceState),最好用_view替换getView()。

试试这个虚拟代码:

在DetailFragment中添加了newInstance:

public static Fragment newInstance(Context cxt, String item) {
   Bundle args = new Bundle();
   args.putString("data", item);
   return Fragment.instantiate(...., args);
}


onViewCreated(....) {
     setText(getArguments().getString("data"));
}

另外,你的detailfragment.xml有问题,根据你的代码,你的linearlayout试图找到自己。我提出了改变你的xml的建议。

<Framelayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> 
    <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:orientation="vertical" 
        android:id="@+id/detailBaseLayout" 
> </LinearLayout> 
</Framelayout>