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>
Related
Just wanted to share an issue that I have with a project that I am doing for a client.
Whenever I enter to my IntroActivity and press the button to take me to MenuActivity, it crashes.
Here is the error log:
02-16 18:49:49.393 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ No view found for id 0x7f090047 (com.wlodsgn.bunbunup:id/linear) for fragment FmMenu{b1e537f0 #0 id=0x7f090047}
02-16 18:49:49.393 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ Activity state:
02-16 18:49:49.423 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ Local FragmentActivity b1e1d1b8 State:
02-16 18:49:49.423 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ mCreated=falsemResumed=false mStopped=false mReallyStopped=false
02-16 18:49:49.423 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ mLoadersStarted=false
02-16 18:49:49.443 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ FragmentManager misc state:
02-16 18:49:49.443 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ mActivity=com.wlodsgn.bunbunup.MenuActivity#b1e1d1b8
02-16 18:49:49.443 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ mContainer=android.support.v4.app.FragmentActivity$2#b1e1ed08
02-16 18:49:49.453 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ mCurState=1 mStateSaved=false mDestroyed=false
02-16 18:49:49.453 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ View Hierarchy:
02-16 18:49:49.453 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ com.android.internal.policy.impl.PhoneWindow$DecorView{b1e23d08 V.E..... ... 0,0-0,0}
02-16 18:49:49.473 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ android.widget.LinearLayout{b1e24280 V.E..... ... 0,0-0,0}
02-16 18:49:49.473 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ android.view.ViewStub{b1e24da8 G.E..... ... 0,0-0,0 #102030e}
02-16 18:49:49.473 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ android.widget.FrameLayout{b1e25038 V.E..... ... 0,0-0,0}
02-16 18:49:49.473 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ android.support.v7.internal.widget.ActionBarOverlayLayout{b1e2db28 V.E..... ... 0,0-0,0 #7f09002f app:id/decor_content_parent}
02-16 18:49:49.483 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ android.support.v7.internal.widget.NativeActionModeAwareLayout{b1e2f758 V.E..... ... 0,0-0,0 #1020002 android:id/content}
02-16 18:49:49.483 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ [ 02-16 18:49:49.513 1208: 1208 E/FragmentManager ]
android.support.v4.widget.DrawerLayout{b1e2c058 VFE..... ... 0,0-0,0 #7f090042 app:id/drawer_layout}
02-16 18:49:49.513 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ android.widget.LinearLayout{b1e1fa90 V.E..... ... 0,0-0,0}
02-16 18:49:49.523 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ android.support.v4.view.ViewPager{b1e29568 VFED.... ... 0,0-0,0 #7f090043 app:id/pager}
02-16 18:49:49.523 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ android.widget.ListView{b1e44148 VFED.VC. ... 0,0-0,0 #7f090044 app:id/listView1}
02-16 18:49:49.523 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ android.widget.ListView{b1dd80b8 VFED.VC. ... 0,0-0,0 #7f090045 app:id/list_slidermenu}
02-16 18:49:49.523 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ android.support.v7.internal.widget.ActionBarContainer{b1e2fcd0 V.ED.... ... 0,0-0,0 #7f090030 app:id/action_bar_container}
02-16 18:49:49.533 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ android.support.v7.widget.Toolbar{b1e30898 V.E..... ... 0,0-0,0 #7f090031 app:id/action_bar}
02-16 18:49:49.533 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ android.widget.ImageButton{b1e39da8 VFED..C. ... 0,0-0,0}
02-16 18:49:49.533 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ android.widget.TextView{b1e28a60 V.ED.... ... 0,0-0,0}
02-16 18:49:49.533 1208-1208/com.wlodsgn.bunbunup E/FragmentManager﹕ android.support.v7.internal.widget.ActionBarContextView{b1e43a50 G.E..... ... 0,0-0,0 #7f090032 app:id/action_context_bar}
02-16 18:49:49.543 1208-1208/com.wlodsgn.bunbunup D/AndroidRuntime﹕ Shutting down VM
02-16 18:49:49.543 1208-1208/com.wlodsgn.bunbunup W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a87ba8)
02-16 18:49:49.573 1208-1208/com.wlodsgn.bunbunup E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.wlodsgn.bunbunup, PID: 1208
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wlodsgn.bunbunup/com.wlodsgn.bunbunup.MenuActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f090047 (com.wlodsgn.bunbunup:id/linear) for fragment FmMenu{b1e537f0 #0 id=0x7f090047}
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
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:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f090047 (com.wlodsgn.bunbunup:id/linear) for fragment FmMenu{b1e537f0 #0 id=0x7f090047}
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:882)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.Activity.performStart(Activity.java:5240)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
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:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Here is my MenuActivity.java where the error is located (Nearly at the end starts with if (fragment != null) { :
import java.util.ArrayList;
import java.util.List;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.view.MenuItemCompat.OnActionExpandListener;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.SearchView.OnQueryTextListener;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
/**
* Created by WiLo on 2/13/2015.
*/
public class MenuActivity extends ActionBarActivity implements OnQueryTextListener, OnActionExpandListener{
/*private TextView texto;*/
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
String[] categoria = {
"Jeans"
};
int[] imagenes = {
R.drawable.veroxjeans1,
R.drawable.veroxjeans2,
R.drawable.veroxjeans3,
R.drawable.veroxjeans4,
R.drawable.veroxjeans5,
R.drawable.veroxjeans6,
R.drawable.veroxjeans7
};
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
/*texto = (TextView) findViewById(R.id.texto);*/
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
// agregar un nuevo item al menu deslizante
// Menu
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Contacto
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Catologo
//navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1), true, "Estrenos"));
// old Contacto (Pedidos)
//navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
// Recycle the typed array
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
//lista
ListView lista = (ListView) findViewById(R.id.listView1);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, categoria );
lista.setAdapter(adapter);
//galeria de imagenes
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[0]));
mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[1]));
mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[2]));
mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[3]));
mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[4]));
mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[5]));
mSectionsPagerAdapter.addfragments(PlaceholderFragment.newInstance(imagenes[6]));
mViewPager.setAdapter(mSectionsPagerAdapter);
}
#Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
Toast.makeText(getApplicationContext(), "Abriendo Busqueda", Toast.LENGTH_SHORT).show();
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
Toast.makeText(getApplicationContext(), "Cerrando Busqueda", Toast.LENGTH_SHORT).show();
return true;
}
#Override
public boolean onQueryTextSubmit(String s) {
/*texto.setText("Buscando...\n\n" + s);*/
return false;
}
#Override
public boolean onQueryTextChange(String s) {
/*texto.setText(" \n\n" + s);*/
return false;
}
/**
* Slide menu item click listener
* */
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.menu3_buscar);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(searchItem, this);
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...
return super.onOptionsItemSelected(item);
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new FmMenu();
break;
case 1:
fragment = new FmContacto();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.linear, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("BunBunUp", "MenuActivity - Error cuando se creo el fragment");
}
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().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 toggle
mDrawerToggle.onConfigurationChanged(newConfig);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
List<android.support.v4.app.Fragment> fragmentos;
public SectionsPagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
fragmentos = new ArrayList<android.support.v4.app.Fragment>();
}
public void addfragments(android.support.v4.app.Fragment xfragment){
fragmentos.add(xfragment);
}
#Override
public android.support.v4.app.Fragment getItem(int position) {
return fragmentos.get(position);
}
#Override
public int getCount() {
return fragmentos.size();
}
}
public static class PlaceholderFragment extends android.support.v4.app.Fragment {
private static final String ARG_IMAGE = "imagen";
private int imagen;
public static PlaceholderFragment newInstance(int imagen) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_IMAGE, imagen);
fragment.setArguments(args);
fragment.setRetainInstance(true);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getArguments() != null) {
imagen = getArguments().getInt(ARG_IMAGE);
}
}
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_menu, container, false);
ImageView imagenView = (ImageView) rootView.findViewById(R.id.imageView1);
imagenView.setImageResource(imagen);
return rootView;
}
}
}
Here are the rest (If needed) of the files that are linked to MenuActivity :
FmMenu.java
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by WiLo on 2/13/2015.
*/
public class FmMenu extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.lay_menufragment, container, false);
return rootView;
}
}
activity_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<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="#ff0a393d">
<!-- Linearlayout to display Fragments -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="265dp"
tools:context=".MenuActivity" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView1"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector"
android:background="#color/list_background"/>
</android.support.v4.widget.DrawerLayout>
fragment_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MenuActivity$PlaceholderFragment"
android:id="#+id/linear">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imageView1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
lay_menufragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
New error log:
02-17 09:41:20.480 917-917/com.wlodsgn.bunbunup W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1ad9ba8)
02-17 09:41:20.560 917-917/com.wlodsgn.bunbunup E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.wlodsgn.bunbunup, PID: 917
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
at android.content.res.Resources.loadDrawable(Resources.java:2110)
at android.content.res.Resources.getDrawable(Resources.java:700)
at android.widget.ImageView.resolveUri(ImageView.java:638)
at android.widget.ImageView.setImageResource(ImageView.java:367)
at com.wlodsgn.bunbunup.MenuActivity$PlaceholderFragment.onCreateView(MenuActivity.java:345)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:851)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
Need more info, let me know
Any help would be appreciated
I think I have found something.
fragmentManager.beginTransaction()
.replace(R.id.linear, fragment).commit();
As you see you try to replace the layout R.id.linear with your fragment. But the R.id.linear is the RelativeLayout you use in your fragment_menu.xml. You should replace the layout in your activity_menu.xml with the current fragment. So I would suggest:
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
And in your activity_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<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="#ff0a393d">
<!-- Linearlayout to display Fragments -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/container">
EDIT:
For the new error: There is a problem with your memory. This often happens when ImageViews are filled by big ressources. So there is only one position where you use an ImageView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_menu, container, false);
ImageView imagenView = (ImageView) rootView.findViewById(R.id.imageView1);
imagenView.setImageResource(imagen);
return rootView;
}
As you see, your imagenView is filled by the resource imagen
imagenView.setImageResource(imagen);
Probably you get the OutOfMemoryError there. As a solution, try this:
try {
imagenView.setImageResource(imagen);
} catch (OutOfMemoryError e) {
//fill your ImageView with something smaller .. maybe a smaller resolution
Log.e("PlaceholderFragment", "Error: OutOfMemoryError")
}
Check your imagen too, whether there is a high resolution needed.
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>
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'm developing an android app with a splash screen. I want another activity which is a tab swiper to be started soon after the splash screen and next go to the main activity.
When I run the following code, my splash screen works fine but then the app stops without showing the tabswiper or the main activity. How should I change my code to get it working? Any suggestions would be highly valued.
My main activity: MainActivity.java
package lk.sidadiya.sidadiya;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = new Intent(MainActivity.this, WelcomeScreen.class);
startActivity(i);
// close this activity
finish();
}
My tabswiper: WelcomeScreen.java
package lk.sidadiya.sidadiya;
import lk.sidadiya.tabswipe.adapter.TabsPagerAdapter;
import lk.sidadiya.sidadiya.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class WelcomeScreen extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
My splash screen: SplashScreen.java
package lk.sidadiya.sidadiya;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class SplashScreen extends Activity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
#Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="lk.sidadiya.sidadiya"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<!-- Splash screen -->
<activity
android:name="lk.sidadiya.sidadiya.SplashScreen"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Welcome screen -->
<activity
android:name="lk.sidadiya.sidadiya.WelcomeScreen"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black" >
<intent-filter>
<action android:name="lk.sidadiya.sidadiya.WelcomeScreen" />
<!-- <category android:name="android.intent.category.DEFAULT" /> -->
</intent-filter>
</activity>
<!-- Main activity -->
<activity
android:name="lk.sidadiya.sidadiya.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.LAUNCHER" />
</intent-filter>
</activity>
-->
</application>
</manifest>
logcat:
04-20 12:09:21.327: I/Process(1684): Sending signal. PID: 1684 SIG: 9
04-20 12:27:56.267: D/dalvikvm(1755): GC_FOR_ALLOC freed 54K, 5% free 2808K/2948K, paused 300ms, total 303ms
04-20 12:27:57.737: D/gralloc_goldfish(1755): Emulator without GPU emulation detected.
04-20 12:28:01.567: D/dalvikvm(1755): GC_FOR_ALLOC freed 9K, 3% free 3311K/3404K, paused 230ms, total 231ms
04-20 12:28:02.997: I/Choreographer(1755): Skipped 38 frames! The application may be doing too much work on its main thread.
04-20 12:28:04.037: D/AndroidRuntime(1755): Shutting down VM
04-20 12:28:04.037: W/dalvikvm(1755): threadid=1: thread exiting with uncaught exception (group=0xb2ad3ba8)
04-20 12:28:04.187: E/AndroidRuntime(1755): FATAL EXCEPTION: main
04-20 12:28:04.187: E/AndroidRuntime(1755): Process: lk.sidadiya.sidadiya, PID: 1755
04-20 12:28:04.187: E/AndroidRuntime(1755): java.lang.RuntimeException: Unable to start activity ComponentInfo{lk.sidadiya.sidadiya/lk.sidadiya.sidadiya.WelcomeScreen}: java.lang.NullPointerException
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.os.Handler.dispatchMessage(Handler.java:102)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.os.Looper.loop(Looper.java:136)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-20 12:28:04.187: E/AndroidRuntime(1755): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 12:28:04.187: E/AndroidRuntime(1755): at java.lang.reflect.Method.invoke(Method.java:515)
04-20 12:28:04.187: E/AndroidRuntime(1755): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-20 12:28:04.187: E/AndroidRuntime(1755): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-20 12:28:04.187: E/AndroidRuntime(1755): at dalvik.system.NativeStart.main(Native Method)
04-20 12:28:04.187: E/AndroidRuntime(1755): Caused by: java.lang.NullPointerException
04-20 12:28:04.187: E/AndroidRuntime(1755): at lk.sidadiya.sidadiya.WelcomeScreen.onCreate(WelcomeScreen.java:34)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.Activity.performCreate(Activity.java:5231)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-20 12:28:04.187: E/AndroidRuntime(1755): ... 11 more
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
when try to click on New Activity Button in my MainActivity , my app crash and closed . whit this error on screen " unfortunately[ App Name ] has stopped "
I google this problem and i can't find any Definitive solution.
here is logCat Log , First Java Source ( MainActivity.java ) and seccond java source ( Seccond.java ) .
Logcat Log :
05-03 00:05:17.750: E/AndroidRuntime(1091): FATAL EXCEPTION: main
05-03 00:05:17.750: E/AndroidRuntime(1091): Process: com.example.helloworld, PID: 1091
05-03 00:05:17.750: E/AndroidRuntime(1091): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld/com.example.helloworld.Seccond}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.helloworld:id/container) for fragment PlaceholderFragment{b2d2faf8 #0 id=0x7f05003c}
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.os.Handler.dispatchMessage(Handler.java:102)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.os.Looper.loop(Looper.java:136)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-03 00:05:17.750: E/AndroidRuntime(1091): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 00:05:17.750: E/AndroidRuntime(1091): at java.lang.reflect.Method.invoke(Method.java:515)
05-03 00:05:17.750: E/AndroidRuntime(1091): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-03 00:05:17.750: E/AndroidRuntime(1091): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-03 00:05:17.750: E/AndroidRuntime(1091): at dalvik.system.NativeStart.main(Native Method)
05-03 00:05:17.750: E/AndroidRuntime(1091): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.helloworld:id/container) for fragment PlaceholderFragment{b2d2faf8 #0 id=0x7f05003c}
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.Activity.performStart(Activity.java:5241)
05-03 00:05:17.750: E/AndroidRuntime(1091): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
MainActivity.java
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Start
Button btn_img = (Button) findViewById(R.id.btn_image);
Button btn = (Button) findViewById(R.id.btn_Click);
final TextView txt = (TextView) findViewById(R.id.txt_view);
Button btn_avtivity = ( Button) findViewById(R.id.btn_activity);
// For button click
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
txt.setText(" Amin Bassam ");
}
});
// Image show
btn_img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
ImageView img=(ImageView)findViewById(R.id.img_view);
img.setImageResource(R.drawable.amin);
}
});
btn_avtivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(),Seccond.class);
startActivity(intent);
}
});
// Open Activities
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new Fragment()).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.
*/
}
Seccond.java
package com.example.helloworld;
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;
public class Seccond extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seccond);
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.seccond, 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.activity_seccond,
container, false);
return rootView;
}
}
}
Activity_main.Xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.helloworld.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/txt_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/btn_Click"
android:layout_alignParentTop="true"
android:layout_marginTop="98dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/btn_Click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="66dp"
android:text="Click"
tools:ignore="HardcodedText" />
"res/layout/activity_main.xml"
<Button
android:id="#+id/btn_image"
style="#style/AppTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn_Click"
android:layout_alignBottom="#+id/btn_Click"
android:layout_marginLeft="16dp"
android:layout_toRightOf="#+id/btn_Click"
android:text="Image"
tools:ignore="HardcodedText" />
<ImageView
android:id="#+id/img_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/btn_image"
android:layout_centerVertical="true"
android:scaleType="center"
android:src="#drawable/abc_ab_solid_light_holo"
tools:ignore="ContentDescription" />
<Button
android:id="#+id/btn_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/btn_image"
android:text="New Activities"
tools:ignore="HardcodedText" />
</RelativeLayout>
Seccond_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#A8B007"
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.helloworld.Seccond$PlaceholderFragment" >
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld"
android:versionCode="1"
android:versionName="1.0" >
<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.helloworld.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.helloworld.Seccond"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
No view found for id 0x7f05003c (com.example.helloworld:id/container) for fragment PlaceholderFragment
The error is saying it cannot find the view with an id of container for the fragment placeholder to go into.
setContentView(R.layout.activity_seccond);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
This part of the code is the main problem. In the add() for the getSupportFragmentManager, it is saying to create the fragment and put it in a view with the id container. But the layout specified in setContentView does not have a view with an id of container. So there needs to be a layout file with a View (like a frame) with an id of container, so that when the fragment layout loads, it can go into that view.
Try adding a file to layouts called activity_frame_seccond.xml with:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Which is a frame with the id 'container'.
Change this line in Seccond.java:
setContentView(R.layout.activity_seccond);
for
setContentView(R.layout.activity_frame_seccond);
and see if that works.
With this code you are using the new file activity_frame_seccond.xml as the layout for the activity, and the first file activity_seccond.xml as the fragment that will put in the frame element (because it has the id container).
If this doesn't work let me know and I will have another look.
Check this line in your Fragment class :
View rootView = inflater.inflate(R.layout.activity_seccond,
container, false);
i think the Returned View is Null
Remove this line from your manifest
<activity
android:name="com.example.helloworld.Second"
android:label="#string/app_name" >
>
</activity>
and try
try in this way
#Override
public void onClick(View arg0) {
Intent intent = new Intent(this,Seccond.class);
startActivity(intent);
}