當我嘗試啓動此活動時,我有一個NPE。此活動工作正常,直到我嘗試添加導航抽屜。我沒有設法找到如何一起完成這項工作。 我把bellow我的創建方法。此活動使用Json解析和抽屜菜單顯示列表視圖以訪問其他活動。無法啓動活動Android
我的代碼:
public class FavoritesActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
private ProgressDialog pDialog;
private static String mylat;
private static String mylng;
// URL to get JSON
private static String url2 = "http://apiurl";
// JSON Node names
private static final String TAG_NAME = "shop_name";
private static final String TAG_ADDRESS_STRING = "shop_address";
private static final String TAG_URL = "shop_url";
private static final String TAG_BOOKLET_URL = "feuilletez";
TextView shop_address;
TextView shop_name;
TextView shop_url;
private LocationManager locMan;
ImageButton feuilletez;
private ListView list;
private String latitude;
private String longitude;
ArrayList<String>shoplatitude;
ArrayList<String>shoplongitude;
ArrayList<String>shopname;
ArrayList<String>shopaddress;
ArrayList<String>shopurl;
// Hashmap for ListView
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorites);
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
// adding nav drawer items to array
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(6, -1), true, "3"));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[7], navMenuIcons.getResourceId(7, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[8], navMenuIcons.getResourceId(8, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[9], navMenuIcons.getResourceId(9, -1)));
// Recycle the typed array
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// Calling async task to get json
new GetJson().execute();
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(1);
}
}
/**
* Slide menu item click listener
* */
public class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
/**
* Async task class to get json by making HTTP call
* */
private class GetJson extends AsyncTask<Void, Void, Void> {
...
的logcat:
01-29 10:00:29.573: E/AndroidRuntime(6923): FATAL EXCEPTION: main
01-29 10:00:29.573: E/AndroidRuntime(6923): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.begital.vitalis/com.begital.vitalis.FavoritesActivity}: java.lang.NullPointerException
01-29 10:00:29.573: E/AndroidRuntime(6923): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-29 10:00:29.573: E/AndroidRuntime(6923): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-29 10:00:29.573: E/AndroidRuntime(6923): at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-29 10:00:29.573: E/AndroidRuntime(6923): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-29 10:00:29.573: E/AndroidRuntime(6923): at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 10:00:29.573: E/AndroidRuntime(6923): at android.os.Looper.loop(Looper.java:137)
01-29 10:00:29.573: E/AndroidRuntime(6923): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-29 10:00:29.573: E/AndroidRuntime(6923): at java.lang.reflect.Method.invokeNative(Native Method)
01-29 10:00:29.573: E/AndroidRuntime(6923): at java.lang.reflect.Method.invoke(Method.java:511)
01-29 10:00:29.573: E/AndroidRuntime(6923): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-29 10:00:29.573: E/AndroidRuntime(6923): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-29 10:00:29.573: E/AndroidRuntime(6923): at dalvik.system.NativeStart.main(Native Method)
01-29 10:00:29.573: E/AndroidRuntime(6923): Caused by: java.lang.NullPointerException
01-29 10:00:29.573: E/AndroidRuntime(6923): at com.begital.vitalis.FavoritesActivity.onCreate(FavoritesActivity.java:145)
01-29 10:00:29.573: E/AndroidRuntime(6923): at android.app.Activity.performCreate(Activity.java:5008)
01-29 10:00:29.573: E/AndroidRuntime(6923): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-29 10:00:29.573: E/AndroidRuntime(6923): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
01-29 10:00:29.573: E/AndroidRuntime(6923): ... 11 more
張貼您的logcat –
我剛添加它。 – Xavier
@Xavier 145線是什麼? –