Вопрос

Hi I followed a tutorial I am trying to test sliding menu (Navigation drawer) in android but I am continuously getting these error in logcat. Please help me or give me some idea so that I can proceed to resolve the issue.HELP................

2-12 18:37:43.976: E/Trace(698): error opening trace file: No such file or directory (2)
02-12 18:37:44.666: I/System.out(698): activity started-----------
02-12 18:37:44.666: I/System.out(698): 3
02-12 18:37:44.676: D/AndroidRuntime(698): Shutting down VM
02-12 18:37:44.676: W/dalvikvm(698): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
02-12 18:37:44.696: E/AndroidRuntime(698): FATAL EXCEPTION: main
02-12 18:37:44.696: E/AndroidRuntime(698): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.navigationdrawertest/com.example.navigationdrawertest.HomeActivity}: java.lang.NullPointerException
02-12 18:37:44.696: E/AndroidRuntime(698):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-12 18:37:44.696: E/AndroidRuntime(698):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-12 18:37:44.696: E/AndroidRuntime(698):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-12 18:37:44.696: E/AndroidRuntime(698):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-12 18:37:44.696: E/AndroidRuntime(698):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 18:37:44.696: E/AndroidRuntime(698):  at android.os.Looper.loop(Looper.java:137)
02-12 18:37:44.696: E/AndroidRuntime(698):  at android.app.ActivityThread.main(ActivityThread.java:4745)
02-12 18:37:44.696: E/AndroidRuntime(698):  at java.lang.reflect.Method.invokeNative(Native Method)
02-12 18:37:44.696: E/AndroidRuntime(698):  at java.lang.reflect.Method.invoke(Method.java:511)
02-12 18:37:44.696: E/AndroidRuntime(698):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-12 18:37:44.696: E/AndroidRuntime(698):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-12 18:37:44.696: E/AndroidRuntime(698):  at dalvik.system.NativeStart.main(Native Method)
02-12 18:37:44.696: E/AndroidRuntime(698): Caused by: java.lang.NullPointerException
02-12 18:37:44.696: E/AndroidRuntime(698):  at com.example.navigationdrawertest.HomeActivity.onCreate(HomeActivity.java:40)
02-12 18:37:44.696: E/AndroidRuntime(698):  at android.app.Activity.performCreate(Activity.java:5008)
02-12 18:37:44.696: E/AndroidRuntime(698):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-12 18:37:44.696: E/AndroidRuntime(698):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
02-12 18:37:44.696: E/AndroidRuntime(698):  ... 11 more
02-12 18:37:48.396: I/Process(698): Sending signal. PID: 698 SIG: 9

Also my HomeActivity.java file

package com.example.navigationdrawertest;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.example.navigationdrawertest.operatingsystemfragment.*;

public class HomeActivity extends Activity {
     private String[] mPlanetTitles;
     private DrawerLayout mDrawerLayout;
     private ListView mDrawerList;
     private ActionBarDrawerToggle mDrawerToggle;
     private CharSequence title;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
         System.out.println("activity started-----------");
        title = getActionBar().getTitle();
        mPlanetTitles = getResources().getStringArray(R.array.operating_systems);
        System.out.println(mPlanetTitles.length);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

     // Set the adapter for the list view
       mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.nav_drawer,R.id.content_frame, mPlanetTitles));
        System.out.println("adapater set to list");
        // Set the list's click listener
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        mDrawerToggle = new ActionBarDrawerToggle(this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_close  /* "close drawer" description */) {


/** Called when a drawer has settled in a completely closed state. */

            public void onDrawerClosed(View view) {
                getActionBar().setTitle(title);
            }


/** Called when a drawer has settled in a completely open state. */

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle("Open Drawer");
            }
        };

       // Set the drawer toggle as the DrawerListener and then Drawer layout will listen on Drawertoggle
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        System.out.println("on create method completed");

    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                System.out.println("DrawerItemClickListener");
                selectItem(position);
            }

    }

    @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);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.home, menu);
      return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Pass the event to ActionBarDrawerToggle, if it returns
        // true, then it has handled the app icon touch event
        if (mDrawerToggle.onOptionsItemSelected(item)) {
          return true;
        }
        // Handle your other action bar items...
        switch (item.getItemId()) {
    case R.id.action_settings:
      Toast.makeText(this, "Settings selected", Toast.LENGTH_LONG).show();
      break;

    default:
      break;
    }
        return super.onOptionsItemSelected(item);
    }


    /** Swaps fragments in the main content view */

    private void selectItem(int position) {
        System.out.println("selectItem called");
        // create a new fragment and specify the planet to show based on position
        Fragment fragment = new OperatingSystemFragment();
        Bundle args = new Bundle();
        args.putInt(OperatingSystemFragment.ARG_OS, position);
        fragment.setArguments(args);

        // Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                       .replace(R.id.content_frame, fragment)
                       .commit();

        // Highlight the selected item, update the title, and close the drawer
        mDrawerList.setItemChecked(position, true);
        getActionBar().setTitle((mPlanetTitles[position]));
        mDrawerLayout.closeDrawer(mDrawerList);
    }    
}

I have to use navigation drawer in API 8 so using support packages. Please help me.

Это было полезно?

Решение

Either your mDrawerList is null or mPlanetTitles is null. Check them both at the debugger.

Другие советы

2-12 18:37:43.976: E/Trace(698): error opening trace file: No such file or directory (2) 

The error You are facing is in this line of code

       mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.nav_drawer,R.id.content_frame, mPlanetTitles));

mPlanetTitles seems to be null

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top