Getting error in my android app - java

I am trying to develop a simple app with navigation drawer.
I do all my best and view so many articles but I failed
There is no error in my code but when I run my app in emulator it shows run time error...
plzz help me
here is my logcat file
12-19 15:07:20.562: E/AndroidRuntime(1067): FATAL EXCEPTION: main
12-19 15:07:20.562: E/AndroidRuntime(1067): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dhruv.sms/com.dhruv.sms.MainActivity}: java.lang.NullPointerException
12-19 15:07:20.562: E/AndroidRuntime(1067): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-19 15:07:20.562: E/AndroidRuntime(1067): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-19 15:07:20.562: E/AndroidRuntime(1067): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-19 15:07:20.562: E/AndroidRuntime(1067): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-19 15:07:20.562: E/AndroidRuntime(1067): at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 15:07:20.562: E/AndroidRuntime(1067): at android.os.Looper.loop(Looper.java:137)
12-19 15:07:20.562: E/AndroidRuntime(1067): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-19 15:07:20.562: E/AndroidRuntime(1067): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 15:07:20.562: E/AndroidRuntime(1067): at java.lang.reflect.Method.invoke(Method.java:511)
12-19 15:07:20.562: E/AndroidRuntime(1067): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-19 15:07:20.562: E/AndroidRuntime(1067): at com.android.internal.os.ZygoteInit.main
(ZygoteInit.java:560)
12-19 15:07:20.562: E/AndroidRuntime(1067): at dalvik.system.NativeStart.main(Native Method)
12-19 15:07:20.562: E/AndroidRuntime(1067): Caused by: java.lang.NullPointerException
12-19 15:07:20.562: E/AndroidRuntime(1067): at com.dhruv.sms.MainActivity.onCreate(MainActivity.java:80)
12-19 15:07:20.562: E/AndroidRuntime(1067): at android.app.Activity.performCreate(Activity.java:5104)
12-19 15:07:20.562: E/AndroidRuntime(1067): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-19 15:07:20.562: E/AndroidRuntime(1067): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-19 15:07:20.562: E/AndroidRuntime(1067): ... 11 more
And this is my java file
package com.dhruv.sms;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
// Within which the entire activity is enclosed
private DrawerLayout mDrawerLayout;
// ListView represents Navigation Drawer
private ListView mDrawerList;
// ActionBarDrawerToggle indicates the presence of Navigation Drawer in the action bar
private ActionBarDrawerToggle mDrawerToggle;
// Title of the action bar
private String mTitle = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = "App title";
getActionBar().setTitle(mTitle);
// Getting reference to the DrawerLayout
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.conversation_list);
// Getting reference to the ActionBarDrawerToggle
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_launcher, R.string.drawer_open,
R.string.drawer_close) {
/** Called when drawer is closed */
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
/** Called when a drawer is opened */
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("App title");
invalidateOptionsMenu();
}
};
// Setting DrawerToggle on DrawerLayout
mDrawerLayout.setDrawerListener(mDrawerToggle);
// Creating an ArrayAdapter to add items to the listview mDrawerList
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(),
R.layout.drawer_list_item, getResources().getStringArray(R.array.sidebar_list_array));
// Setting the adapter on mDrawerList
mDrawerList.setAdapter(adapter);
// Enabling Home button
getActionBar().setHomeButtonEnabled(true);
// Enabling Up navigation
getActionBar().setDisplayHomeAsUpEnabled(true);
// Setting item click listener for the listview mDrawerList
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Getting an array of rivers
String[] menuItems = getResources().getStringArray(R.array.sidebar_list_array);
// Currently selected river
mTitle = menuItems[position];
// Creating a fragment object
Fragment rFragment = new Fragment();
// Passing selected item information to fragment
// Getting reference to the FragmentManager
FragmentManager fragmentManager = getFragmentManager();
// Creating a fragment transaction
FragmentTransaction ft = fragmentManager.beginTransaction();
// Adding a fragment to the fragment transaction
ft.replace(R.id.content_frame, rFragment);
// Committing the transaction
ft.commit();
// Closing the drawer
mDrawerLayout.closeDrawer(mDrawerList);
}
});
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
/** Called whenever we call invalidateOptionsMenu() */
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

The variable mDrawerList might not be initialised correctly. It could be null, thus causing the NullPointerException.

There is an error in your code. The stack trace shows that. And here's the relevant information
12-19 15:07:20.562: E/AndroidRuntime(1067): Caused by: java.lang.NullPointerException
12-19 15:07:20.562: E/AndroidRuntime(1067): at com.dhruv.sms.MainActivity.onCreate(MainActivity.java:80)
Line 80 shows mDrawerList being accessed. It's null

the error is in this part of your code
mDrawerList.setAdapter(adapter);
mDrawerList reference might be wrong or adapter is wrong

private ListView mDrawerList=new ListView();
You haven't initialized the ListView.

Related

error java app Android

I have a problem and I do not understand the reason why. I'm working on Android Studio on Linux. The bitmap size is 10x10 pixels. I searched in google, but it gave me nothing. I would be grateful if someone could help me.
This problem is something with this:
"Bitmap suweren = BitmapFactory.decodeResource(getResources(), R.drawable.suweren);"
and this is error log:
"12-30 07:16:36.125 9336-9336/com.example.szczesciarz.sawa E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.szczesciarz.sawa, PID: 9336
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.szczesciarz.sawa/com.example.szczesciarz.sawa.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v7.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:98)
at android.support.v7.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:91)
at android.support.v7.app.ToolbarActionBar.<init>(ToolbarActionBar.java:73)
at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:205)
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
at com.example.szczesciarz.sawa.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
12-30 07:21:36.565 9336-9336/com.example.szczesciarz.sawa I/Process: Sending signal. PID: 9336 SIG: 9"
Main Activity_code:
package com.example.szczesciarz.sawa;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GameView(this));
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#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) {
// 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);
}
}
GameView code:
package com.example.szczesciarz.sawa;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;
class GameView extends View{
public GameView(Context context){
super(context);
}
#Override
public void onDraw(Canvas canvas ){
Bitmap suweren = BitmapFactory.decodeResource(Context.getResources(), R.drawable.suweren);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(suweren, 0, 0, null);
}
}
Thanks):-
Toolbar cannot be found :
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
So when you call
setSupportActionBar(toolbar);
You have a NullPointerException at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
try to remove setSupportActionBar(toolbar)
Make sure that your layout file contains toolbar or you have included the toolbar layout.
This exception occurs when your layout file does not contain the id, so android is unable to get the view from that id and returns null when you call findViewById(R.id.toolbar);

Nullpointer exception - FragmentActivity

I have received a nullpointer exception error in my fragmentactivity.
Below is the log cat:
11-04 06:29:51.550: E/AndroidRuntime(19286): FATAL EXCEPTION: main
11-04 06:29:51.550: E/AndroidRuntime(19286): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dooba.beta/com.dooba.beta.MainActivity}: java.lang.NullPointerException
11-04 06:29:51.550: E/AndroidRuntime(19286): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-04 06:29:51.550: E/AndroidRuntime(19286): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-04 06:29:51.550: E/AndroidRuntime(19286): at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-04 06:29:51.550: E/AndroidRuntime(19286): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-04 06:29:51.550: E/AndroidRuntime(19286): at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 06:29:51.550: E/AndroidRuntime(19286): at android.os.Looper.loop(Looper.java:137)
11-04 06:29:51.550: E/AndroidRuntime(19286): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-04 06:29:51.550: E/AndroidRuntime(19286): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 06:29:51.550: E/AndroidRuntime(19286): at java.lang.reflect.Method.invoke(Method.java:511)
11-04 06:29:51.550: E/AndroidRuntime(19286): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-04 06:29:51.550: E/AndroidRuntime(19286): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-04 06:29:51.550: E/AndroidRuntime(19286): at dalvik.system.NativeStart.main(Native Method)
11-04 06:29:51.550: E/AndroidRuntime(19286): Caused by: java.lang.NullPointerException
11-04 06:29:51.550: E/AndroidRuntime(19286): at com.dooba.beta.MainActivity.onCreate(MainActivity.java:51)
11-04 06:29:51.550: E/AndroidRuntime(19286): at android.app.Activity.performCreate(Activity.java:5008)
11-04 06:29:51.550: E/AndroidRuntime(19286): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-04 06:29:51.550: E/AndroidRuntime(19286): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-04 06:29:51.550: E/AndroidRuntime(19286): ... 11 more
I believe the error occurs within these lines:
mDrawerList.setAdapter(new ArrayAdapter<String>(getActionBar()
.getThemedContext(), android.R.layout.simple_list_item_1,
mOptionMenu));
Below is the entire activity code:
import android.app.ActionBar;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
private String[] mOptionMenu;
private DrawerLayout mDrawerLayout;
private RelativeLayout mDrawerRelativeLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mTitleSection;
private CharSequence mTitleApp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mOptionMenu = new String[] { "Opción 1", "Opción 2", "Opción 3" };
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerRelativeLayout = (RelativeLayout)
findViewById(R.id.left_drawer);
mDrawerList = (ListView) findViewById(R.id.list_view_drawer);
mDrawerList.setAdapter(new ArrayAdapter<String>(getActionBar()
.getThemedContext(), android.R.layout.simple_list_item_1,
mOptionMenu));
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new FirstFragment();
break;
case 1:
fragment = new SecondFragment();
break;
case 2:
fragment = new ThirdFragment();
break;
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mTitleSection = mOptionMenu[position];
getActionBar().setTitle(mTitleSection);
mDrawerLayout.closeDrawer(mDrawerRelativeLayout);
}
});
mDrawerList.setItemChecked(0, true);
mTitleSection = getTitle();
mTitleApp = getTitle();
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitleSection);
ActivityCompat.invalidateOptionsMenu(MainActivity.this);
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mTitleSection);
ActivityCompat.invalidateOptionsMenu(MainActivity.this);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
;
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
If any clarification is needed, let me know.
Thanks in advance.
Since you are importing import android.support.v7.app.ActionBarActivity , I guess you should use
getSupportActionBar()
instead of
getActionBar()
Instead of
getActionBar().getThemedContext()
use this

Android app crashing with OnClickListener in fragment

I'm having a problem building my first App. I'm using Navigation drawer to switch between fragments. But when i choose OrderFragment from Navigation drawer, app crashes.
OrderFragment has only one button showing a toast when clicked, but the app crashes before the OrderFragment loads.
The code is:
MainActivity.java
package com.example.testcocogeek;
import android.app.Activity;
import android.app.Fragment;
import android.app.SearchManager;
import android.content.Intent;
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.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
private String[] mListOptions;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mListOptions = getResources().getStringArray(R.array.list_options);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Set adapter for the list view
mDrawerList.setAdapter (new ArrayAdapter<String> (this,
R.layout.drawer_list_item, mListOptions));
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// Set the list click listener
mDrawerList.setOnItemClickListener (new DrawerItemClickListener());
// 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, "Cocogeek");
// 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 listener 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);
}
}
// This method selects correct fragment.
private void selectItem(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
Bundle args = new Bundle();
switch (position) {
case 0:
fragment = new CocogeekFragment();
break;
case 1:
fragment = new OrderFragment();
break;
case 2:
fragment = new LocatorFragment();
break;
case 3:
fragment = new ContactFragment();
break;
default:
break;
}
fragment.setArguments(args);
android.app.FragmentManager frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, fragment)
.commit();
mDrawerList.setItemChecked(position, true);
setTitle(mListOptions[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
#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 toggles
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
OrderFragment.java
package com.example.testcocogeek;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class OrderFragment extends Fragment implements OnClickListener {
public OrderFragment() {
// Empty constructor required for fragment subclasses
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mView = (View) inflater.inflate(R.layout.order_fragment, container, false);
// Initialize UI views.
final Button send = (Button) getActivity().findViewById(R.id.send);
//Initialize OnClick listener.
send.setOnClickListener(this);
return mView;
}
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Clicked" ,Toast.LENGTH_LONG).show();
}
}
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"
android:background="#FFF5EE"
>
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</FrameLayout>
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#858585"/>
</android.support.v4.widget.DrawerLayout>
order_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/send"
/>
</LinearLayout>
logCat:
07-08 12:28:09.332: D/dalvikvm(30471): GC_FOR_ALLOC freed 165K, 2% free 16940K/17136K, paused 15ms, total 15ms
07-08 12:28:09.362: I/dalvikvm-heap(30471): Grow heap (frag case) to 35.315MB for 19655120-byte allocation
07-08 12:28:09.372: D/dalvikvm(30471): GC_FOR_ALLOC freed 2K, 1% free 36132K/36332K, paused 11ms, total 11ms
07-08 12:28:09.522: I/Adreno-EGL(30471): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
07-08 12:28:09.542: D/OpenGLRenderer(30471): Enabling debug mode 0
07-08 12:28:11.852: D/AndroidRuntime(30471): Shutting down VM
07-08 12:28:11.852: W/dalvikvm(30471): threadid=1: thread exiting with uncaught exception (group=0x41625ba8)
07-08 12:28:11.862: E/AndroidRuntime(30471): FATAL EXCEPTION: main
07-08 12:28:11.862: E/AndroidRuntime(30471): Process: com.example.testcocogeek, PID: 30471
07-08 12:28:11.862: E/AndroidRuntime(30471): java.lang.NullPointerException
07-08 12:28:11.862: E/AndroidRuntime(30471): at com.example.testcocogeek.OrderFragment.onCreateView(OrderFragment.java:29)
07-08 12:28:11.862: E/AndroidRuntime(30471): at android.app.Fragment.performCreateView(Fragment.java:1700)
07-08 12:28:11.862: E/AndroidRuntime(30471): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
07-08 12:28:11.862: E/AndroidRuntime(30471): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
07-08 12:28:11.862: E/AndroidRuntime(30471): at android.app.BackStackRecord.run(BackStackRecord.java:684)
07-08 12:28:11.862: E/AndroidRuntime(30471): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
07-08 12:28:11.862: E/AndroidRuntime(30471): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
07-08 12:28:11.862: E/AndroidRuntime(30471): at android.os.Handler.handleCallback(Handler.java:733)
07-08 12:28:11.862: E/AndroidRuntime(30471): at android.os.Handler.dispatchMessage(Handler.java:95)
07-08 12:28:11.862: E/AndroidRuntime(30471): at android.os.Looper.loop(Looper.java:136)
07-08 12:28:11.862: E/AndroidRuntime(30471): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-08 12:28:11.862: E/AndroidRuntime(30471): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 12:28:11.862: E/AndroidRuntime(30471): at java.lang.reflect.Method.invoke(Method.java:515)
07-08 12:28:11.862: E/AndroidRuntime(30471): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-08 12:28:11.862: E/AndroidRuntime(30471): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-08 12:28:11.862: E/AndroidRuntime(30471): at dalvik.system.NativeStart.main(Native Method)
07-08 12:28:13.332: I/Process(30471): Sending signal. PID: 30471 SIG: 9
At first sight you should replace:
final Button send = (Button) getActivity().findViewById(R.id.send);
with:
final Button send = (Button) mView.findViewById(R.id.send);
R.id.send id is contained in the Fragment view mView.

Android: Navigation Drawer NullPointerException

I am using code from the developer website (http://developer.android.com/training/implementing-navigation/nav-drawer.html) to install a navigation drawer, however, I am getting a NullPointerException in this method:
/* 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_search).setVisible(!drawerOpen);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
Here is the error
04-14 20:51:39.720: D/dalvikvm(826): GC_FOR_ALLOC freed 93K, 6% free 3119K/3284K, paused 95ms, total 96ms
04-14 20:51:39.970: I/Choreographer(826): Skipped 69 frames! The application may be doing too much work on its main thread.
04-14 20:51:40.360: I/Choreographer(826): Skipped 256 frames! The application may be doing too much work on its main thread.
04-14 20:51:40.440: D/gralloc_goldfish(826): Emulator without GPU emulation detected.
04-14 20:51:40.730: D/AndroidRuntime(826): Shutting down VM
04-14 20:51:40.730: W/dalvikvm(826): threadid=1: thread exiting with uncaught exception (group=0xb2ac7ba8)
04-14 20:51:40.740: E/AndroidRuntime(826): FATAL EXCEPTION: main
04-14 20:51:40.740: E/AndroidRuntime(826): Process: com.example.myfirstapp, PID: 826
04-14 20:51:40.740: E/AndroidRuntime(826): java.lang.NullPointerException
04-14 20:51:40.740: E/AndroidRuntime(826): at com.example.myfirstapp.MainActivity.onPrepareOptionsMenu(MainActivity.java:139)
04-14 20:51:40.740: E/AndroidRuntime(826): at android.app.Activity.onPreparePanel(Activity.java:2556)
04-14 20:51:40.740: E/AndroidRuntime(826): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:464)
04-14 20:51:40.740: E/AndroidRuntime(826): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
04-14 20:51:40.740: E/AndroidRuntime(826): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
04-14 20:51:40.740: E/AndroidRuntime(826): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
04-14 20:51:40.740: E/AndroidRuntime(826): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
04-14 20:51:40.740: E/AndroidRuntime(826): at android.view.Choreographer.doFrame(Choreographer.java:543)
04-14 20:51:40.740: E/AndroidRuntime(826): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
04-14 20:51:40.740: E/AndroidRuntime(826): at android.os.Handler.handleCallback(Handler.java:733)
04-14 20:51:40.740: E/AndroidRuntime(826): at android.os.Handler.dispatchMessage(Handler.java:95)
04-14 20:51:40.740: E/AndroidRuntime(826): at android.os.Looper.loop(Looper.java:136)
04-14 20:51:40.740: E/AndroidRuntime(826): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-14 20:51:40.740: E/AndroidRuntime(826): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 20:51:40.740: E/AndroidRuntime(826): at java.lang.reflect.Method.invoke(Method.java:515)
04-14 20:51:40.740: E/AndroidRuntime(826): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-14 20:51:40.740: E/AndroidRuntime(826): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-14 20:51:40.740: E/AndroidRuntime(826): at dalvik.system.NativeStart.main(Native Method)
Here is the rest of the main activity
package com.example.myfirstapp;
import java.util.Locale;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.SearchManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
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.navigation_drawer_options);
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_search).setVisible(!drawerOpen);
menu.findItem(R.id.action_settings).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) {
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
#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);
}
}
menu xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.myfirstapp.MainActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
</menu>
Your menu.xml has only one time with id= action_settings. You should add another menu item for search.
example:
You can use searchView
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView" />

Error while building test sliding menu

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

Categories