Bit of a noob at android, but made it this far. Trying to get a ListView with navigation drawer... The following doesn't work unless I uncomment the setContentView line. Throws a null ptr exception. When I uncomment it, the ListView works, but the nav drawer quits showing up. Also, the onListItemClick doesn't trigger, but that's a later problem for me to learn.
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
public class All_Listview extends ListFragment {
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.d("onClick", String.valueOf(position));
Activity a = getActivity();
String item = (String) getListAdapter().getItem(position);
Toast.makeText(a, item + " selected", Toast.LENGTH_LONG).show();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayList<WeekItem> wkList = new ArrayList<>();
Activity a = getActivity();
// a.setContentView(R.layout.all_listview);
WeekAdapter adapter = new WeekAdapter(a.getBaseContext(), wkList);
ListView lv = (ListView)a.findViewById(R.id.all_list_view);
lv.setAdapter(adapter);
SQLiteHelper db = new SQLiteHelper(getActivity());
List<Week> weeks = db.getAll();
ArrayList<WeekItem> wkItem = WeekItem.fromList(weeks);
adapter.addAll(wkItem);
}
}
I'm sure there's more to this problem than I can see at the moment, but any help is welcomed.
MainActivity
package com.hourtracker;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.support.v4.widget.DrawerLayout;
public class MainActivity extends ActionBarActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the
* navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in
* {@link #restoreActionBar()}.
*/
private CharSequence mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("hours","creating session filter");
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
.findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
Log.d("onNavDrawerSelected", String.valueOf(position));
FragmentManager fragmentManager = getSupportFragmentManager();
switch(position) {
case 0:
fragmentManager
.beginTransaction()
.replace(R.id.frame_container,
((Fragment)new Entry_Fragment())).commit();
break;
case 1:
fragmentManager
.beginTransaction()
.replace(R.id.frame_container,
((Fragment)new Week_Fragment())).commit();
break;
case 2:
fragmentManager
.beginTransaction()
.replace(R.id.frame_container,
((Fragment)new All_Listview())).commit();
break;
}
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager
.beginTransaction()
.replace(R.id.frame_container,
((Fragment)new Settings_Fragment())).commit();
return true;
}
Toast.makeText(this, "Not settings", Toast.LENGTH_SHORT).show();;
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<!--
As the main content view, the view below consumes the entire
space available using match_parent in both dimensions.
-->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--
android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead.
-->
<!--
The drawer is given a fixed width in dp and extends the full height of
the container.
-->
<fragment
android:id="@+id/navigation_drawer"
android:name="com.hourtracker.NavigationDrawerFragment"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
all_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/all_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
I think the main error is when you create All_Listview that extends ListFragment, i suggest that you follow this example that helps you create Simple ListFragment
After you do it right i think the list fragment will work and even the navigation drawer too, thats because when you setContentView from ListFragment you do it wrong