来自Fragment的Activity中的findViewById

时间:2014-08-20 20:01:46

标签: android android-fragments

编辑:该死的,我只是注意到每次从片段返回主Activity时历史都会重置,所以我无法看到下面的操作是否真的有效。我需要查看我的代码。


onClick上的“coin2”必须在“历史”TextView中附加一些文字。 “coin2”按钮位于片段中,而“history”位于MainActivity中。我无法使用findViewById访问该TextView,我读了很多解决方案但没有工作,因为在“case R.id.coin2”中,历史和rootView都无法解析。 CardBackFragment类实际上在MainActivity.java文件中,而不是单独的文件。

fragment.xml之:

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

<Button
    android:id="@+id/coin2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:text="@string/coin_button"
    android:onClick="flipcoin2" />
</LinearLayout>

片段类:

public class CardBackFragment extends Fragment {


    public CardBackFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_coindice, container, false);
        v.findViewById(R.id.coin2).setOnClickListener(this);
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        TextView history = (TextView) rootView.findViewById(R.id.history);
        return v;

    }

    @Override
    public void onClick(View view){
        switch (view.getId())
        {
            case R.id.coin2:
                //history and rootView cannot be resolved.
                history = (TextView) rootView.findViewById(R.id.history);
                final String[] coin = {getString(R.string.head), getString(R.string.tail)};
                Random r = new Random();
                final int flip = r.nextInt(2);
                history.append("\n- " + getString(R.string.coin_message) + coin[flip]);

            break;
        }

    }

2 个答案:

答案 0 :(得分:0)

您无法从另一个布局中访问导致异常的其他视图NPE。

<强>溶液

textviewbuttons放入fragment_main xml,以便{text} findViewById或以编程方式手动将textView添加到父视图中。

注意不要只在膨胀1 布局中为片段中的两个布局充气。

如果textView在活动内。

您可以直接从片段中调用getActivity().findViewById

<强>样品:

    switch (view.getId())
    {
        case R.id.coin2:
            //history and rootView cannot be resolved.
            history = (TextView) CardBackFragment.this.getActivity().findViewById(R.id.history);
            final String[] coin = {getString(R.string.head), getString(R.string.tail)};
            Random r = new Random();
            final int flip = r.nextInt(2);
            history.append("\n- " + getString(R.string.coin_message) + coin[flip]);

        break;
    }

答案 1 :(得分:0)

你试过

吗?
    public class CardBackFragment extends Fragment {

public interface OnCoinClickedListener {
void onCoinClicked(String stringToAppend);
}

        public CardBackFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_coindice, container, false);
        }

@Override
public void onAttach(Activity activity) {
mListener = (OnCoinClickedListener)activity;
}

@Override
public void onDetach() {
mListener = null;
}
        @Override
    public void onViewCreated(View v, Bundle b) {
            v.findViewById(R.id.coin2).setOnClickListener(this);
    }

        @Override
        public void onClick(View view){
            switch (view.getId())
            {
                case R.id.coin2:
                    final String[] coin = {getString(R.string.head), getString(R.string.tail)};
                    Random r = new Random();
                    final int flip = r.nextInt(2);
mListener.onCoinClicked("\n- " + getString(R.string.coin_message) + coin[flip]);
                break;
            }

        }

然后,在您的活动中:

public class CoinActivity extends Activity implements OnCoinClickedListener{

private TextVIew mHistory;

[...] onCreate {
mHistory = (TextView)findviewById(R.id.history);
}

public void onCoinClicked(String s) {
mHistory.append(s);

}