0

我使用 listfragment 并尝试更改它的内容。这是我的活动代码:

public class MainActivity extends Activity {
    public static Context appContext;
    public ToFragment ToFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.activity_main);
        ActionBar actionbar = getActionBar();
        actionbar.setDisplayShowTitleEnabled(false);
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.Tab FromTab = actionbar.newTab().setText("FromFragment");
        ActionBar.Tab ToTab = actionbar.newTab().setText("ToFragment");

        FromFragment FromFragment = new FromFragment();
        ToFragment ToFragment = new ToFragment();

        ToTab.setTabListener(new MyTabsListener(ToFragment));
        FromTab.setTabListener(new MyTabsListener(FromFragment));

//        ListView tolistview = ToFragment.getListView();
//        ListView fromlistview = FromFragment.getListView();

        actionbar.addTab(ToTab);
        actionbar.addTab(FromTab);
    }

      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.mainmenu, menu);
        return true;
      }

      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_refresh:
            String[] countries = new String[] {
                    "Russia",
                    "USA",
                };  
            ToFragment.changeContent(countries);
          break;
        case R.id.action_settings:

          break;

        default:
          break;
        }

        return true;
      }
}

这里我初始化了两个listfragment,ToFragment 和FromFragment。然后在这里 (onCreateOptionsMenu) 我尝试通过我的函数 changeContent 更改 ToFragment 的内容。这是片段的代码:

public class ToFragment extends ListFragment {
    String[] countries = new String[] {
            "India",
            "Pakistan",
        };
    private LayoutInflater inflater;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.inflater = inflater;
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(), android.R.layout.simple_list_item_1, countries);

        setListAdapter(adapter);

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    public void changeContent(String[] array) {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(), android.R.layout.simple_list_item_1, array);
        setListAdapter(adapter);
    }

如您所见,我试图让函数 changeContent 像函数 onCreateView 一样工作。但!我的问题是它在这里抛出空指针异常,我真的很困惑,我无法意识到问题所在!你能告诉我有什么问题吗?或者你能建议更好的方法来改变 listfragment 内容吗?

这是错误跟踪:

04-08 22:48:33.292: I/System.out(8817): adapter == null
04-08 22:48:33.292: W/dalvikvm(8817): threadid=1: thread exiting with uncaught exception (group=0x414572d0)
04-08 22:48:33.297: E/AndroidRuntime(8817): FATAL EXCEPTION: main
04-08 22:48:33.297: E/AndroidRuntime(8817): java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.rkarasev.miptrain/ru.rkarasev.miptrain.MainActivity}: java.lang.NullPointerException
04-08 22:48:33.297: E/AndroidRuntime(8817):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
04-08 22:48:33.297: E/AndroidRuntime(8817):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
04-08 22:48:33.297: E/AndroidRuntime(8817):     at android.app.ActivityThread.access$600(ActivityThread.java:151)
04-08 22:48:33.297: E/AndroidRuntime(8817):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
04-08 22:48:33.297: E/AndroidRuntime(8817):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-08 22:48:33.297: E/AndroidRuntime(8817):     at android.os.Looper.loop(Looper.java:155)
04-08 22:48:33.297: E/AndroidRuntime(8817):     at android.app.ActivityThread.main(ActivityThread.java:5493)
04-08 22:48:33.297: E/AndroidRuntime(8817):     at java.lang.reflect.Method.invokeNative(Native Method)
04-08 22:48:33.297: E/AndroidRuntime(8817):     at java.lang.reflect.Method.invoke(Method.java:511)
04-08 22:48:33.297: E/AndroidRuntime(8817):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
04-08 22:48:33.297: E/AndroidRuntime(8817):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
04-08 22:48:33.297: E/AndroidRuntime(8817):     at dalvik.system.NativeStart.main(Native Method)
04-08 22:48:33.297: E/AndroidRuntime(8817): Caused by: java.lang.NullPointerException
04-08 22:48:33.297: E/AndroidRuntime(8817):     at ru.rkarasev.miptrain.ToFragment.changeContent(ToFragment.java:34)
04-08 22:48:33.297: E/AndroidRuntime(8817):     at ru.rkarasev.miptrain.MainActivity.onCreate(MainActivity.java:49)
04-08 22:48:33.297: E/AndroidRuntime(8817):     at android.app.Activity.performCreate(Activity.java:5066)
04-08 22:48:33.297: E/AndroidRuntime(8817):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
04-08 22:48:33.297: E/AndroidRuntime(8817):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
04-08 22:48:33.297: E/AndroidRuntime(8817):     ... 11 more
04-08 22:48:35.382: D/Process(8817): killProcess, pid=8817
04-08 22:48:35.387: D/Process(8817): dalvik.system.VMStack.getThreadStackTrace(Native Method)
04-08 22:48:35.387: D/Process(8817): java.lang.Thread.getStackTrace(Thread.java:599)
04-08 22:48:35.387: D/Process(8817): android.os.Process.killProcess(Process.java:944)
04-08 22:48:35.392: D/Process(8817): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:108)
04-08 22:48:35.392: D/Process(8817): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
04-08 22:48:35.392: D/Process(8817): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
04-08 22:48:35.392: D/Process(8817): dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

更改创建ArrayAdapter实例的方式:

ArrayList<String> arrayList = new ArrayList<String>();
arrayList.addAll(Arrays.asList(countries));

ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(), android.R.layout.simple_list_item_1, arrayList);

如在此答案中找到:Unable to modify ArrayAdapter in ListView: UnsupportedOperationException

public void changeContent(String[] array) {
    ArrayAdapter<String> adapter = (ArrayAdapter<String>)getListAdapter();
    if (adapter != null) {
        adapter.clear();
        adapter.addAll(Arrays.asList(array));
        adapter.notifyDataSetChanged();
    }
}

您不需要每次都重新创建适配器。只需清除并设置新数组,然后通知适配器数据集已更改,以便更新您的视图。

于 2013-04-08T00:19:04.267 回答