I am following a tutorial to creating a sliding navigation with a drawer navigation.
Android Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aa.slide"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.aa.slide.MainActivity"
android:label="#string/app_name"
android:theme="#style/Theme.MyCompatTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Below is my MainActivity.java
package com.aa.slide;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
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;
// Adopted from: https://developer.android.com/training/implementing-navigation/nav-drawer.html
public class MainActivity extends ActionBarActivity {
private String[] mPlanetTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private CharSequence mTitle;
private ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = "test";
mPlanetTitles = new String[]{"one", "two", "three"};
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.drawer_list_item, mPlanetTitles));
// 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(mTitle);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mTitle);
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#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 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...
return super.onOptionsItemSelected(item);
}
/**
* Swaps fragments in the main content view
*/
private void selectItem(int position) {
Toast.makeText(this, R.string.app_name, Toast.LENGTH_SHORT).show();
// Highlight the selected item, update the title, and close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
selectItem(position);
}
}
}
Upon Executing the above codes, I keep recieving this error in my log
08-14 13:18:18.632: W/dalvikvm(12483): Unable to resolve superclass of Lcom/aa/slide/MainActivity; (11)
08-14 13:18:18.632: W/dalvikvm(12483): Link of class 'Lcom/aa/slide/MainActivity;' failed
08-14 13:18:18.632: D/AndroidRuntime(12483): Shutting down VM
08-14 13:18:18.632: W/dalvikvm(12483): threadid=1: thread exiting with uncaught exception (group=0x4113c2a0)
08-14 13:18:18.637: E/AndroidRuntime(12483): FATAL EXCEPTION: main
08-14 13:18:18.637: E/AndroidRuntime(12483): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.aa.slide/com.aa.slide.MainActivity}: java.lang.ClassNotFoundException: com.aa.slide.MainActivity
08-14 13:18:18.637: E/AndroidRuntime(12483): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
08-14 13:18:18.637: E/AndroidRuntime(12483): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
08-14 13:18:18.637: E/AndroidRuntime(12483): at android.app.ActivityThread.access$700(ActivityThread.java:140)
08-14 13:18:18.637: E/AndroidRuntime(12483): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
08-14 13:18:18.637: E/AndroidRuntime(12483): at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 13:18:18.637: E/AndroidRuntime(12483): at android.os.Looper.loop(Looper.java:137)
08-14 13:18:18.637: E/AndroidRuntime(12483): at android.app.ActivityThread.main(ActivityThread.java:4921)
08-14 13:18:18.637: E/AndroidRuntime(12483): at java.lang.reflect.Method.invokeNative(Native Method)
08-14 13:18:18.637: E/AndroidRuntime(12483): at java.lang.reflect.Method.invoke(Method.java:511)
08-14 13:18:18.637: E/AndroidRuntime(12483): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
08-14 13:18:18.637: E/AndroidRuntime(12483): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
08-14 13:18:18.637: E/AndroidRuntime(12483): at dalvik.system.NativeStart.main(Native Method)
08-14 13:18:18.637: E/AndroidRuntime(12483): Caused by: java.lang.ClassNotFoundException: com.aa.slide.MainActivity
08-14 13:18:18.637: E/AndroidRuntime(12483): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
08-14 13:18:18.637: E/AndroidRuntime(12483): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
08-14 13:18:18.637: E/AndroidRuntime(12483): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
08-14 13:18:18.637: E/AndroidRuntime(12483): at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
08-14 13:18:18.637: E/AndroidRuntime(12483): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2025)
08-14 13:18:18.637: E/AndroidRuntime(12483): ... 11 more
UPDATE
this is my build.gradle
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.Android.support:appcompat-v7:21.0.+'
}
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
Please how can i solve this problem of unable to instantiate activity? Thanks
What exceptions throws
java.lang.ClassNotFoundException: com.aa.slide.MainActivity
ClassNotFoundException occurs when class loader could not find the required class in class path . So , basically you should check your class path and add the class in the classpath.
Rebuild and Clean up your Project.Please check This SO Answer.
For your information,Since the version 22.1.0, the class ActionBarActivity is deprecated. You should use AppCompatActivity or Actvity . I hope it helps you .
Be sure that this is in your gradle file:
dependencies {
compile 'com.android.support:appcompat-v7:21.0.0'
}
The problem is that the dalvikVM can't find import android.support.v7.app.ActionBarActivity; which is the superclass of your MainActivity
You can see this in:
08-14 13:18:18.632: W/dalvikvm(12483): Unable to resolve superclass of Lcom/aa/slide/MainActivity; (11)
Try this in Manifest file:
**android:name=".MainActivity"**
<activity
android:name="com.aa.slide.MainActivity"
android:label="#string/app_name"
android:theme="#style/Theme.MyCompatTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Related
I have created a project in Android Studio:
package com.example.apps.newapplication;
Java code:
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;
import android.widget.RelativeLayout;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout DakotasLayout = new RelativeLayout(this);
Button redButton = new Button(this);
DakotasLayout.addView(redButton);
setContentView(DakotasLayout);
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);
}
}
Manifest.xml code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app.newapplication">
<application>
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The problem is my app shuts down every time I launch it in the emulator.
I'm not sure what's wrong in my java file. It might be my manifest file that's wrong, but I'm not sure.
Here's the logcat:
Error Logs:
01-13 18:24:51.244 4486-4486/? I/art: Not late-enabling -Xcheck:jni (already on)
01-13 18:24:51.328 4486-4494/? I/art: Debugger is no longer active
01-13 18:24:51.369 4486-4494/? W/art: Suspending all threads took: 41.636ms
01-13 18:24:51.373 4486-4486/? W/System: ClassLoader referenced unknown path: /data/app/com.example.apps.newapplication-2/lib/x86
01-13 18:24:51.423 4486-4486/? D/AndroidRuntime: Shutting down VM
01-13 18:24:51.424 4486-4486/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.apps.newapplication, PID: 4486
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.apps.newapplication/com.example.apps.newapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
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.apps.newapplication.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-13 18:25:03.649 4486-4494/com.example.apps.newapplication W/art: Suspending all threads took: 60.192ms
01-13 18:25:22.653 4486-4494/com.example.apps.newapplication W/art: Suspending all threads took: 44.409ms
01-13 18:25:29.152 4486-4494/com.example.apps.newapplication W/art: Suspending all threads took: 24.137ms
The problem is you are not setting a valid layout for what your activity is expecting. Your layout needs to have in it a defined view for each call to findViewById(...); you have. The view's ID will match what you pass into the method.
Currently you are only setting your content view to a relative layout with a button so you are likely getting a NullPointerException when you lookup other views using findViewById(...); and later try to use them.
Look inside your res/layout directory for a valid layout file and use that as your content view instead since this looks like a template to begin with.
setContentView(R.layout.activity_main);
I am trying to make a navigation drawer but every time it's executed the application crashes. I am a beginner. Please, let me know if you need any more info and thanks
MainActivity.java
package com.pixalstudio.navigationdrawer;
import android.app.Activity;
import android.content.Intent;
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.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class MainActivity extends Activity implements OnItemClickListener {
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] mTitles;
DrawerLayout mDrawerLayout;
ListView mDrawerList;
mTitles = getResources().getStringArray(R.array.drawerItems);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.drawer_listview);
mDrawerList.setAdapter((ListAdapter) new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mTitles));
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.closeDrawers();
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void selectItem(int position) {
switch (position) {
case 0:
Intent i = new Intent(MainActivity.this, LocationSelection.class);
startActivity(i);
break;
default:
}
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
switch (position) {
case 0:
Intent i = new Intent(MainActivity.this, LocationSelection.class);
startActivity(i);
break;
default:
}
}
}
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.pixalstudio.navigationdrawer.MainActivity" >
<RelativeLayout
android:id="#+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#757575" >
</RelativeLayout>
<ListView
android:id="#+id/drawer_listview"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#FF9800"
android:choiceMode="singleChoice" >
</ListView>
</android.support.v4.widget.DrawerLayout>
LogCat
07-07 16:53:20.301: D/OpenGLRenderer(1775): Use EGL_SWAP_BEHAVIOR_PRESERVED: true
07-07 16:53:20.305: D/(1775): HostConnection::get() New Host Connection established 0xb42d7750, tid 1775
07-07 16:53:20.322: D/Atlas(1775): Validating map...
07-07 16:53:20.398: D/libEGL(1775): loaded /system/lib/egl/libEGL_emulation.so
07-07 16:53:20.399: D/libEGL(1775): loaded /system/lib/egl/libGLESv1_CM_emulation.so
07-07 16:53:20.414: D/libEGL(1775): loaded /system/lib/egl/libGLESv2_emulation.so
07-07 16:53:20.427: D/(1775): HostConnection::get() New Host Connection established 0xaf039480, tid 1793
07-07 16:53:20.638: I/OpenGLRenderer(1775): Initialized EGL, version 1.4
07-07 16:53:20.728: D/OpenGLRenderer(1775): Enabling debug mode 0
07-07 16:53:20.900: W/EGL_emulation(1775): eglSurfaceAttrib not implemented
07-07 16:53:20.900: W/OpenGLRenderer(1775): Failed to set EGL_SWAP_BEHAVIOR on surface 0xaf035860, error=EGL_SUCCESS
07-07 16:53:20.940: D/AndroidRuntime(1775): Shutting down VM
07-07 16:53:20.940: D/AndroidRuntime(1775): --------- beginning of crash
07-07 16:53:20.941: E/AndroidRuntime(1775): FATAL EXCEPTION: main
07-07 16:53:20.941: E/AndroidRuntime(1775): Process: com.pixalstudio.navigationdrawer, PID: 1775
07-07 16:53:20.941: E/AndroidRuntime(1775): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.pixalstudio.navigationdrawer/com.pixalstudio.navigationdrawer.LocationSelection}: java.lang.ClassCastException: com.pixalstudio.navigationdrawer.LocationSelection cannot be cast to android.app.Activity
07-07 16:53:20.941: E/AndroidRuntime(1775): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
07-07 16:53:20.941: E/AndroidRuntime(1775): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
07-07 16:53:20.941: E/AndroidRuntime(1775): at android.app.ActivityThread.access$800(ActivityThread.java:151)
07-07 16:53:20.941: E/AndroidRuntime(1775): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
07-07 16:53:20.941: E/AndroidRuntime(1775): at android.os.Handler.dispatchMessage(Handler.java:102)
07-07 16:53:20.941: E/AndroidRuntime(1775): at android.os.Looper.loop(Looper.java:135)
07-07 16:53:20.941: E/AndroidRuntime(1775): at android.app.ActivityThread.main(ActivityThread.java:5254)
07-07 16:53:20.941: E/AndroidRuntime(1775): at java.lang.reflect.Method.invoke(Native Method)
07-07 16:53:20.941: E/AndroidRuntime(1775): at java.lang.reflect.Method.invoke(Method.java:372)
07-07 16:53:20.941: E/AndroidRuntime(1775): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
07-07 16:53:20.941: E/AndroidRuntime(1775): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
07-07 16:53:20.941: E/AndroidRuntime(1775): Caused by: java.lang.ClassCastException: com.pixalstudio.navigationdrawer.LocationSelection cannot be cast to android.app.Activity
07-07 16:53:20.941: E/AndroidRuntime(1775): at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
07-07 16:53:20.941: E/AndroidRuntime(1775): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
07-07 16:53:20.941: E/AndroidRuntime(1775): ... 10 more
07-07 16:53:23.310: I/Process(1775): Sending signal. PID: 1775 SIG: 9
Menifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pixalstudio.navigationdrawer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LocationSelection"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.LOCATIONSELECTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Hi I am new to Android stuff.Right now I'm following the tutorial given in developer.android.com/training/basics/firstapp/starting-activity.html and building my fist app.It was fine until I dealt with single activity.Later I have added second activity.When I click the button in first it should be directed to second activity.But on clicking first activity my app stops suddenly.Please help me.
here's the logcat
06-23 23:10:50.134: E/FragmentManager(437): No view found for id
0x7f05003c (com.example.honey:id/container) for fragment
PlaceholderFragment{43e98050 #0 id=0x7f05003c} 06-23 23:10:50.134:
E/FragmentManager(437): Activity state: 06-23 23:10:50.264:
E/AndroidRuntime(437): FATAL EXCEPTION: main 06-23 23:10:50.264:
E/AndroidRuntime(437): java.lang.RuntimeException: Unable to start
activity
ComponentInfo{com.example.honey/com.example.honey.DisplayMessageActivity}:
java.lang.IllegalArgumentException: No view found for id 0x7f05003c
(com.example.honey:id/container) for fragment
PlaceholderFragment{43e98050 #0 id=0x7f05003c} 06-23 23:10:50.264:
E/AndroidRuntime(437): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-23 23:10:50.264: E/AndroidRuntime(437): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-23 23:10:50.264: E/AndroidRuntime(437): at
android.app.ActivityThread.access$2300(ActivityThread.java:125) 06-23
23:10:50.264: E/AndroidRuntime(437): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-23 23:10:50.264: E/AndroidRuntime(437): at
android.os.Handler.dispatchMessage(Handler.java:99) 06-23
23:10:50.264: E/AndroidRuntime(437): at
android.os.Looper.loop(Looper.java:123) 06-23 23:10:50.264:
E/AndroidRuntime(437): at
android.app.ActivityThread.main(ActivityThread.java:4627) 06-23
23:10:50.264: E/AndroidRuntime(437): at
java.lang.reflect.Method.invokeNative(Native Method) 06-23
23:10:50.264: E/AndroidRuntime(437): at
java.lang.reflect.Method.invoke(Method.java:521) 06-23 23:10:50.264:
E/AndroidRuntime(437): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-23 23:10:50.264: E/AndroidRuntime(437): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 06-23
23:10:50.264: E/AndroidRuntime(437): at
dalvik.system.NativeStart.main(Native Method) 06-23 23:10:50.264:
E/AndroidRuntime(437): Caused by: java.lang.IllegalArgumentException:
No view found for id 0x7f05003c (com.example.honey:id/container) for
fragment PlaceholderFragment{43e98050 #0 id=0x7f05003c} 06-23
23:10:50.264: E/AndroidRuntime(437): at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:930)
06-23 23:10:50.264: E/AndroidRuntime(437): at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
06-23 23:10:50.264: E/AndroidRuntime(437): at
android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-23 23:10:50.264: E/AndroidRuntime(437): at
android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
06-23 23:10:50.264: E/AndroidRuntime(437): at
android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
06-23 23:10:50.264: E/AndroidRuntime(437): at
android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
06-23 23:10:50.264: E/AndroidRuntime(437): at
android.app.Activity.performStart(Activity.java:3781) 06-23
23:10:50.264: E/AndroidRuntime(437): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
06-23 23:10:50.264: E/AndroidRuntime(437): ... 11 more
1st activity java file
package com.example.honey;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
static final String EXTRA_MESSAGE = "com.example.honey.MESSAGE";
public void sendMessage(View view){ Intent intent=new
Intent(this,DisplayMessageActivity.class); EditText
editText=(EditText)findViewById(R.id.edit_query); String message =
editText.getText().toString(); intent.putExtra(EXTRA_MESSAGE,
message); startActivity(intent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new
PlaceholderFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
}
1st activity fragment_main.xml
For 2nd activity
package com.example.honey;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View; import
android.view.ViewGroup;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); Intent intent=getIntent();
setContentView(R.layout.activity_display_message);
String message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView =new TextView(this);
textView.setText(message);
setContentView(textView);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_display_message,container, false);
return rootView;
}
}
}
XML file
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.honey.DisplayMessageActivity$PlaceholderFragment"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView> </LinearLayout>
manifest
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.honey.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.honey.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.first.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.first.MainActivity" />
</activity>
</application>
You're making a silly error in the second activity code due to copy pasting:
DisplayMessageActivity:
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
You're trying to add the PlaceholderFragment to the R.id.container, when the content view of the second activity is the TextView, which does NOT have the container, aka the layout specified by R.id.container does not exist in the second activity.
Remove these lines from the second activity and it should work.
You forgot to add the activity to the manifest I think...
<activity
android:name="DisplayMessageActivity"
android:label="#string/..." >
</activity>
Check your XML layout for MainActivity, you specify findViewById(R.id.edit_query) for your EditText which was never declared.
Add the stared line to your fragment_main.xml file
`<EditText
**android:id="#+id/edit_query**
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message"
android:layout_weight="1"/>`
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" />
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