访问片段内的视图

时间:2013-12-30 14:32:10

标签: android fragment nullreferenceexception android-fragmentactivity

我在访问片段中的视图时遇到了麻烦。在下面的示例中,我无法访问Fragment中的第二个按钮(例如, findViewById 似乎返回NULL并且当我尝试b2.setText(“test”)时应用程序崩溃),但是当我直接在activity_main.xml中添加它时,它确实有效。

以下是代码:

MainActivity.java

public class MainActivity extends Activity 
{

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

    final Button fragmentButton = (Button)findViewById(R.id.iMainButton);

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.iMainInnerLLContainer, new TestFragment());         
    ft.commit();

    final Button b2 = (Button)findViewById(R.id.iTestFragmentInnerButton);
    if(b2 == null) { Log.d("MainActivity.java:", "b2 is null"); } 
    else { Log.d("MainActivity.java:", "b2 is NOT null"); } 

    fragmentButton.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            // do stuff here
        }
    });
}

}

activity_main.xml中

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

<Button
    android:id="@+id/iMainButton"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:text="Add fragment..." 
/>    

<LinearLayout
    android:id="@+id/iMainInnerLLContainer"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="vertical"
>
    <!-- Fragment goes here, can reference button if it is added here manually -->
</LinearLayout>

</LinearLayout>

TestFragment.java

public class TestFragment extends Fragment 
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    return inflater.inflate(R.layout.ltestfragment, container, false);
}
}

ltestfragment.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/iTestFragmentOuterLinearLayout"
>

<Button
    android:id="@+id/iTestFragmentInnerButton"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:text="This is my test fragment button." 
/>
</LinearLayout>

我认为我在这里遗漏了一些非常基本的东西,我希望能够对这可能是什么有所了解。

提前谢谢!

2 个答案:

答案 0 :(得分:0)

我认为您需要在片段中设置textchangelistener。

public class FragmentA extends Fragment {

TextChangeListener listener;

public interface TextChangeListener {
    public void onTextChange(CharSequence newText);
}

public void setTextChangeListener(TextChangeListener listener) {
    this.listener = listener;
}

然后,在您的活动中,设置监听器:

public class ActivityAB extends FragmentActivity {

FragmentA fragmentA;
FragmentB fragmentB;

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

    FragmentManager manager = getSupportFragmentManager();
    fragmentA = (FragmentA) manager.findFragmentById(R.id.fragmentA);
    fragmentB = (FragmentB) manager.findFragmentById(R.id.fragmentB);

    fragmentA.setTextChangeListener(new TextChangeListener() {

        @Override
        public void onTextChange(CharSequence newText) {
            fragmentB.updateTextValue(newText);
        }
    });
}

}

答案 1 :(得分:0)

实现一个接口,以从其活动中侦听片段的事件。

片段中的代码: -

   public class TestFragment extends Fragment  
   {
       public interface OnClickListener 
       {
            public void onButtonClicked(<data type><data>);  
       }

       b2.setOnClickListener(new OnClickListener() {
       @Override    
            public void onClick(View v) {
            listener.onButtonClicked(<<pass some values here>>);
            }
       });
   }

主要代码: -

   MainActivity extends FragmentActivity implements TestFragment.OnClickListener, 
   {
        @Override
        public void onButtonClicked(<<receive the pased data here>>) {

        //do some stuff here with the received data after the button is clicked
    } 
   }