碎片onAttach / Create / CreateView方法未调用

时间:2013-01-16 16:59:44

标签: android android-fragments nullpointerexception initialization android-support-library

我正在写一个简单的Activity,它应该检索并显示包含Fragment的{​​{1}}中的一些数据。问题是我无法访问ListView,因为似乎没有调用任何ListView方法。

该片段有on*,由LinearLayout中的代码动态填充。 onCreateViewActivity中创建Fragment,并使用onCreate返回的FragmentManager将其添加到其布局中(作为旁注:我正在使用support-v4 library)。

getSupportFragmentManager / Log.d / onAttach方法中添加一些onCreate来电,清楚地表明根本没有调用它们。

我做错了什么?

活动:

onCreateView

活动布局:

public class CharactersActivity extends FragmentActivity {

    private MainCharactersFragment mainFragment = null;

    private String[] charnames = null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // R.layout.characters contains only a LinearLayout with id characters_fragment_container
        setContentView(R.layout.characters);
        if (mainFragment == null) {
            mainFragment = new MainCharactersFragment();
            //Add the fragment to the activity's layout.
            //Taken from Google's tutorials.
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.characters_fragment_container, mainFragment).commit();
            //I'd expect onCreateView to be called during this commit, but it is not called.
        }
        //Loads some data to display
        Intent intent = getIntent();
        String[] filenames = intent.getStringArrayExtra(PortableDnDActivity.CHARACTERS_FILENAMES);
        this.charnames = new String[filenames.length];
        for (int i=0; i < filenames.length; i++) {
            this.charnames[i] = getCharacterName(filenames[i]);
        }
    }

    @Override
    public void onStart() {
        // Set the ListAdapter to show the data.
        Log.d("MyLog", "           HERE            ");  // Shown by logs
        ListView view = (ListView) findViewById(R.id.main_characters_view); // This is null
        Log.d("MyLog", "The view is null:" + (view == null));
        mainFragment.setCharsAdapter(this, this.charnames);
        Log.d("MyLog", "                 HERE           2222"); //NOT shown by the logs
    }

片段:

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

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

片段布局:

public class MainCharactersFragment extends Fragment {

    public ListView charsList;
    public Button newCharButton;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // The following is never shown in the logs
        Log.d("MyLog", "                HERE                ");
        View view = inflater.inflate(R.layout.main_characters_fragment, container, false);
        return view;
    }

    public void setCharsAdapter(Context ctx, String[] data) {
        // Raise NullPointerException because at this moment
        // findViewById(R.id.main_characters_view) returns null
        charsList.setAdapter(new ArrayAdapter<String>(ctx, R.id.main_characters_view, data));
    }
    @Override
    public void onAttach(Activity activity) {
        Log.d("MyLog", "            onAttach called           "); //Never shown
    }
}

1 个答案:

答案 0 :(得分:2)

我认为NPE令人困惑。评论您的onStart(),以便您可以单独解决。

一旦你这样做,你会发现有一些情况下要调用super方法。进入MainCharactersFragment.onAttach()后进入CharactersActivity.onStart()。添加这两个调用,清理,然后重试。

鉴于这些更改,您的代码对我来说运行正常。