1

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

该片段有一个LinearLayoutonCreateView. Activity创建Fragmentin并使用返回的 by将onCreate其添加到其布局中(附带说明:我使用的是 support-v4 库)。FragmentManagergetSupportFragmentManager

在//方法中添加一些Log.d调用清楚地表明它们根本没有被调用。onAttachonCreateonCreateView

我究竟做错了什么?

活动:

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
    }
}

片段布局:

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

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

    <ListView android:id="@+id/main_characters_view"
              android:layout_width="fill_parent"
              android:layout_height="0dp"
              android:layout_weight="0.5"
              />
    <Button android:id="@+id/new_character_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/new_character"
            />

</LinearLayout>
4

1 回答 1

2

我认为 NPE 令人困惑。将其注释掉,onStart()以便您可以单独解决。

一旦你这样做了,你会看到你有几个跌倒的情况来调用该super方法。一进MainCharactersFragment.onAttach()一进CharactersActivity.onStart()。添加这两个调用,清理,然后重试。

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

于 2013-01-16T17:22:47.263 回答