使用NavigationDrawer后Listview不显示任何内容

时间:2018-09-02 06:24:24

标签: android listview

我是android编程的新手,我的项目是从Web上获取RSS Feed,它工作正常,然后我将该应用程序移至了导航抽屉菜单中。 然后,该应用程序将无法运行,我确定代码上没有错误,但是在onActivityCreated()方法中“我认为!”。无论如何,这是我的代码

主要活动:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
NavigationView navigationView=null;
Toolbar toolbar=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RssFragment fragment=new RssFragment();
    android.support.v4.app.FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container,fragment);
    fragmentTransaction.commit();
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

RssFragment:

public class RssFragment extends Fragment implements AdapterView.OnItemClickListener {

private ProgressBar progressBar;
private ListView listView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_rss, container, false);
    progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
    listView = (ListView) view.findViewById(R.id.listView);
    listView.setOnItemClickListener(this);
    final SwipeRefreshLayout pullToRefresh = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
    pullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            startService();
            pullToRefresh.setRefreshing(false);
        }
    });
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    startService();
}

private void startService() {
    Intent intent = new Intent(getActivity(), RssService.class);
    getActivity().startService(intent);
}

/**
 * Once the {@link RssService} finishes its task, the result is sent to this BroadcastReceiver
 */
private BroadcastReceiver resultReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        progressBar.setVisibility(View.GONE);
        List<RssItem> items = (List<RssItem>) intent.getSerializableExtra(RssService.ITEMS);
        if (items != null) {
            RssAdapter adapter = new RssAdapter(getActivity(), items);
            listView.setAdapter(adapter);
        } else {
            Toast.makeText(getActivity(), "An error occurred while downloading the rss feed.",
                    Toast.LENGTH_LONG).show();
        }
    }
};

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    RssAdapter adapter = (RssAdapter) parent.getAdapter();
    RssItem item = (RssItem) adapter.getItem(position);
    Uri uri = Uri.parse(item.getLink());
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
}
@Override
public void onStart() {
    super.onStart();
    IntentFilter intentFilter = new IntentFilter(RssService.ACTION_RSS_PARSED);
    LocalBroadcastManager.getInstance(getActivity()).registerReceiver(resultReceiver, intentFilter);
}

@Override
public void onStop() {
    super.onStop();
    LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(resultReceiver);
}

app_bar_main:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/fragment_container"
    android:layout_height="match_parent" />

这是应用程序中的主要活动,在转到导航抽屉主要活动之前,该活动调用RssFragment:

public class MainActivity extends AppCompatActivity {
@Override   
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    if (savedInstanceState == null) {
        addRssFragment();
    }
}
private void addRssFragment() {

    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    RssFragment fragment = new RssFragment();
    transaction.add(R.id.fragment_container, fragment);
    transaction.commit();
}
}

注意:该应用程序运行良好,但是当我将其移至导航抽屉时,该应用程序无法正常工作,活动也没有显示任何内容,只需滑动即可刷新小部件。

0 个答案:

没有答案