gpt4 book ai didi

android - Android应用程序中抽屉导航上的空指针异常

转载 作者:行者123 更新时间:2023-11-29 00:10:49 25 4
gpt4 key购买 nike

我正在学习本教程: http://codetheory.in/android-navigation-drawer/

当应用程序启动时,我得到:java.lang.RuntimeException: 无法启动 Activity ComponentInfo{pakage/pakage.MainActivity}: java.lang.NullPointerException...引起原因:java.lang.NullPointerException 在 pakage.MainActivity.onPostCreate(MainActivity.java:138)

第138行标有注释//第138行

代码:

package net.udazz.udazz;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {

ListView mDrawerList;
RelativeLayout mDrawerPane;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;

ArrayList<NavItem> mNavItems = new ArrayList<NavItem>();

private WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mNavItems.add(new NavItem("Home", "Meetup destination", R.mipmap.ic_action_home));
mNavItems.add(new NavItem("Preferences", "Change your preferences", R.mipmap.ic_action_settings));
mNavItems.add(new NavItem("About", "Get to know about us", R.mipmap.ic_action_about));

// DrawerLayout
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);

// Populate the Navigtion Drawer with options
mDrawerPane = (RelativeLayout) findViewById(R.id.drawerPane);
mDrawerList = (ListView) findViewById(R.id.navList);
DrawerListAdapter adapter = new DrawerListAdapter(this, mNavItems);
mDrawerList.setAdapter(adapter);

// Drawer Item click listeners
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItemFromDrawer(position);
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

mWebView = (WebView) findViewById(R.id.activity_main_webview);

// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);

mWebView.loadUrl("http://page/");

// Stop local links and redirects from opening in browser instead of WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
}

@Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle
// If it returns true, then it has handled
// the nav drawer indicator touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}

// 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();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
/*
* Called when a particular item from the navigation drawer
* is selected.
* */
private void selectItemFromDrawer(int position) {
Fragment fragment = new PreferencesFragment();

FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.mainContent, fragment)
.commit();

mDrawerList.setItemChecked(position, true);
setTitle(mNavItems.get(position).mTitle);

// Close the drawer
mDrawerLayout.closeDrawer(mDrawerPane);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState(); // line 138
}
}
class NavItem {
String mTitle;
String mSubtitle;
int mIcon;

public NavItem(String title, String subtitle, int icon) {
mTitle = title;
mSubtitle = subtitle;
mIcon = icon;
}
}
class DrawerListAdapter extends BaseAdapter {

Context mContext;
ArrayList<NavItem> mNavItems;

public DrawerListAdapter(Context context, ArrayList<NavItem> navItems) {
mContext = context;
mNavItems = navItems;
}

@Override
public int getCount() {
return mNavItems.size();
}

@Override
public Object getItem(int position) {
return mNavItems.get(position);
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;

if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.drawer_item, null);
}
else {
view = convertView;
}

TextView titleView = (TextView) view.findViewById(R.id.title);
TextView subtitleView = (TextView) view.findViewById(R.id.subTitle);
ImageView iconView = (ImageView) view.findViewById(R.id.icon);

titleView.setText(mNavItems.get(position).mTitle);
subtitleView.setText(mNavItems.get(position).mSubtitle);
iconView.setImageResource(mNavItems.get(position).mIcon);

return view;
}
}

最佳答案

您忘记在onCreate() 方法中初始化mDrawerToggle:

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.drawer_open,
R.string.drawer_close) {

public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
}

public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
};

参数:

> this                      The Activity hosting the drawer
> mDrawerLayout The DrawerLayout to link to the given Activity's ActionBar
> R.string.drawer_open A String resource to describe the "open drawer" action for accessibility
> R.string.drawer_close A String resource to describe the "close drawer" action for accessibility

关于android - Android应用程序中抽屉导航上的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30267200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com