我已經下載了gridview代碼和導航欄的代碼。 這兩個代碼單獨工作正常,但是當我試圖將它們結合給它一個運行時錯誤「不幸的應用程序已停止」。gridview和導航抽屜在android中不能一起工作
下面是我的代碼:
MainActivity.java
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
switch(item.getItemId()) {
case R.id.action_websearch:
// create intent to perform web search for this planet
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());
// catch event that there's no activity to handle intent
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
// update the main content by replacing fragments
Fragment fragment = new PlanetFragment();
Bundle args = new Bundle();
args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
/**
* Fragment that appears in the "content_frame", shows a planet
*/
public static class PlanetFragment extends Fragment {
public static final String ARG_PLANET_NUMBER = "planet_number";
GridView gridview;
public PlanetFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
gridview=(GridView) rootView.findViewById(R.id.gridview);
ImageAdapter i= new ImageAdapter(this);
gridview.setAdapter(i);
return rootView;
}
}
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private static final Context PlanetFragment = null;
private PlanetFragment mContext;
public ImageAdapter(PlanetFragment c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(PlanetFragment);
imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(4, 4, 4, 4);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
private Integer[] mThumbIds = {
R.drawable.snehalaya , R.drawable.prayas_11,
R.drawable.vs , R.drawable.adr,
R.drawable.anandvan , R.drawable.bgbb,
R.drawable.bngvn , R.drawable.dilasa ,
R.drawable.hohk , R.drawable.jivan,
R.drawable.mahan,R.drawable.vishva,
R.drawable.saraswati , R.drawable.samaj,
R.drawable.atma
};
}
activity_main.xml中
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
drawer_list_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#fff"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
fragment_planet.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="6"
android:background="#000000"
>
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="424dp"
android:layout_marginTop="10dp"
android:background="#000000"
android:columnWidth="120dp"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="10dp" />
</LinearLayout>
的logcat:
01-25 17:27:48.637: E/Trace(892): error opening trace file: No such file or directory (2)
01-25 17:27:49.558: E/AndroidRuntime(892): FATAL EXCEPTION: main
01-25 17:27:49.558: E/AndroidRuntime(892): java.lang.NullPointerException
01-25 17:27:49.558: E/AndroidRuntime(892): at android.view.ViewConfiguration.get(ViewConfiguration.java:318)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.view.View.<init>(View.java:3234)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.widget.ImageView.<init>(ImageView.java:105)
01-25 17:27:49.558: E/AndroidRuntime(892): at com.example.android.navigationdrawerexample.ImageAdapter.getView(ImageAdapter.java:35)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.widget.GridView.onMeasure(GridView.java:1040)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.view.View.measure(View.java:15513)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.view.View.measure(View.java:15513)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.view.View.measure(View.java:15513)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:615)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.view.View.measure(View.java:15513)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.view.View.measure(View.java:15513)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.view.View.measure(View.java:15513)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-25 17:27:49.558: E/AndroidRuntime(892): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.view.View.measure(View.java:15513)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.view.Choreographer.doFrame(Choreographer.java:532)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.os.Handler.handleCallback(Handler.java:725)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.os.Looper.loop(Looper.java:137)
01-25 17:27:49.558: E/AndroidRuntime(892): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-25 17:27:49.558: E/AndroidRuntime(892): at java.lang.reflect.Method.invokeNative(Native Method)
01-25 17:27:49.558: E/AndroidRuntime(892): at java.lang.reflect.Method.invoke(Method.java:511)
01-25 17:27:49.558: E/AndroidRuntime(892): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-25 17:27:49.558: E/AndroidRuntime(892): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-25 17:27:49.558: E/AndroidRuntime(892): at dalvik.system.NativeStart.main(Native Method)
顯示你的崩潰日誌? –
嗨阿米特我已經添加logcat的代碼 – user3228219
檢查我的答案,你必須使用活力或上下文實例。 –