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"/>`
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 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>
This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 8 years ago.
I am new to this android. Whenever I click the login button, it says "unfortunately, your app has stopped".
There is no compile time error..
I am uploading my MainActivity.java, LoginActivity.java and manifest.xml files..
Please tell me whats the problem or am i doing something wrong?
//MainActivity.java
package com.satty002.swag;
import java.util.Locale;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.parse.ParseAnalytics;
import com.parse.ParseUser;
public class MainActivity extends Activity implements ActionBar.TabListener {
public static final String TAG = MainActivity.class.getSimpleName();
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {#link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {#link android.support.v13.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ParseAnalytics.trackAppOpened(getIntent());
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser == null) {
navigateToLogin();
} else {
Log.i(TAG, currentUser.getUsername());
}
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
private void navigateToLogin() {
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
#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_logout) {
ParseUser.logOut();
navigateToLogin();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return 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;
}
}
}
//LoginActivity.java
package com.satty002.swag;
import android.app.Activity;
import android.app.AlertDialog;
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.EditText;
import android.widget.TextView;
import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseUser;
public class LoginActivity extends Activity {
protected EditText mUsername;
protected EditText mPassword;
protected Button mLoginButton;
protected TextView mSignUpTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mSignUpTextView = (TextView) findViewById(R.id.SignupText);
mSignUpTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this, SignupActivity.class);
startActivity(intent);
}
});
mUsername = (EditText) findViewById(R.id.usernameField);
mPassword = (EditText) findViewById(R.id.passwordFiled);
mLoginButton = (Button) findViewById(R.id.loginButton);
mLoginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String username = mUsername.getText().toString();
String password = mPassword.getText().toString();
username.trim();
password.trim();
if(username.isEmpty() || password.isEmpty() )
{
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setMessage(R.string.login_error_message)
.setTitle(R.string.login_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
else
{
ParseUser.logInInBackground(username, password, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
if(e == null)
{
//success
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
else
{
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setMessage(R.string.login_error_message)
.setTitle(R.string.login_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.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.login, 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);
}
}
//manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.satty002.swag"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" android:name="SwagApplication">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".SignupActivity"
android:label="#string/app_name"
android:parentActivityName=".LoginActivity">
</activity>
</application>
//Activity_login.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="com.satty002.swag.LoginActivity" >
<TextView
android:id="#+id/SignupText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/signup_text" />
<EditText
android:id="#+id/usernameField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="#string/username_hint" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/PasswordField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/usernameField"
android:layout_below="#+id/usernameField"
android:ems="10"
android:hint="#string/password_hint"
android:inputType="textPassword" />
<Button
android:id="#+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/PasswordField"
android:layout_below="#+id/PasswordField"
android:text="#string/login_button_label" />
//logcat
07-25 17:52:52.332: E/Trace(1627): error opening trace file: No such file or directory (2)
07-25 17:52:54.393: D/dalvikvm(1627): GC_CONCURRENT freed 274K, 7% free 6132K/6535K, paused 14ms+110ms, total 330ms
07-25 17:52:55.083: D/libEGL(1627): loaded /system/lib/egl/libEGL_emulation.so
07-25 17:52:55.093: D/(1627): HostConnection::get() New Host Connection established 0xb8462cf0, tid 1627
07-25 17:52:55.123: D/libEGL(1627): loaded /system/lib/egl/libGLESv1_CM_emulation.so
07-25 17:52:55.153: D/libEGL(1627): loaded /system/lib/egl/libGLESv2_emulation.so
07-25 17:52:55.363: W/EGL_emulation(1627): eglSurfaceAttrib not implemented
07-25 17:52:55.423: D/OpenGLRenderer(1627): Enabling debug mode 0
07-25 17:52:56.833: W/EGL_emulation(1627): eglSurfaceAttrib not implemented
07-25 17:52:56.833: I/Choreographer(1627): Skipped 37 frames! The application may be doing too much work on its main thread.
07-25 17:52:57.283: D/dalvikvm(1627): GC_CONCURRENT freed 189K, 5% free 6342K/6663K, paused 18ms+101ms, total 151ms
07-25 17:53:19.003: D/AndroidRuntime(1627): Shutting down VM
07-25 17:53:19.003: W/dalvikvm(1627): threadid=1: thread exiting with uncaught exception (group=0xb5f03288)
07-25 17:53:19.014: E/AndroidRuntime(1627): FATAL EXCEPTION: main
07-25 17:53:19.014: E/AndroidRuntime(1627): java.lang.NullPointerException
07-25 17:53:19.014: E/AndroidRuntime(1627): at com.satty002.swag.LoginActivity$2.onClick(LoginActivity.java:50)
07-25 17:53:19.014: E/AndroidRuntime(1627): at android.view.View.performClick(View.java:4084)
07-25 17:53:19.014: E/AndroidRuntime(1627): at android.view.View$PerformClick.run(View.java:16966)
07-25 17:53:19.014: E/AndroidRuntime(1627): at android.os.Handler.handleCallback(Handler.java:615)
07-25 17:53:19.014: E/AndroidRuntime(1627): at android.os.Handler.dispatchMessage(Handler.java:92)
07-25 17:53:19.014: E/AndroidRuntime(1627): at android.os.Looper.loop(Looper.java:137)
07-25 17:53:19.014: E/AndroidRuntime(1627): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-25 17:53:19.014: E/AndroidRuntime(1627): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 17:53:19.014: E/AndroidRuntime(1627): at java.lang.reflect.Method.invoke(Method.java:511)
07-25 17:53:19.014: E/AndroidRuntime(1627): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-25 17:53:19.014: E/AndroidRuntime(1627): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-25 17:53:19.014: E/AndroidRuntime(1627): at dalvik.system.NativeStart.main(Native Method)
I am going to guess that there is a null pointer somewhere in your onClick for your login activity. you should be able to figure this out by looking at your logcat which should tell you the exact line of code that is causing this crash. probably referencing an object that is not initialized / null
Edit
As stated you have a null reference on line 50 of LoginActivity:
java.lang.NullPointerException
07-25 17:53:19.014: E/AndroidRuntime(1627): at com.satty002.swag.LoginActivity$2.onClick(LoginActivity.java:50
you misspelled the password field reference:
mPassword = (EditText) findViewById(R.id.passwordFiled);
should be
mPassword = (EditText) findViewById(R.id.passwordField);
Line 50 in your LoginActivity throws NullPointerException,
line 50 will be somewhere around:
String username = mUsername.getText().toString();
String password = mPassword.getText().toString();
which means mUsername or mPassword is null
Check are you binding right layout to activity, and are you using correct ids
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);
}
MainActivity.java
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.content.Intent;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal" >
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
DisplayMessageActivity.java
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
#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;
}
}
}
Error Log
04-10 01:37:00.460: W/dalvikvm(1650): threadid=1: thread exiting with uncaught exception (group=0xb3aaeba8)
04-10 01:37:00.520: E/AndroidRuntime(1650): FATAL EXCEPTION: main
04-10 01:37:00.520: E/AndroidRuntime(1650): Process: com.example.myfirstapp, PID: 1650
04-10 01:37:00.520: E/AndroidRuntime(1650): java.lang.IllegalStateException: Could not find a method sendMessage(View) in the activity class com.example.myfirstapp.MainActivity for onClick handler on view class android.widget.Button
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.view.View$1.onClick(View.java:3810)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.view.View.performClick(View.java:4438)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.view.View$PerformClick.run(View.java:18422)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.os.Handler.handleCallback(Handler.java:733)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.os.Handler.dispatchMessage(Handler.java:95)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.os.Looper.loop(Looper.java:136)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-10 01:37:00.520: E/AndroidRuntime(1650): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 01:37:00.520: E/AndroidRuntime(1650): at java.lang.reflect.Method.invoke(Method.java:515)
04-10 01:37:00.520: E/AndroidRuntime(1650): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-10 01:37:00.520: E/AndroidRuntime(1650): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-10 01:37:00.520: E/AndroidRuntime(1650): at dalvik.system.NativeStart.main(Native Method)
04-10 01:37:00.520: E/AndroidRuntime(1650): Caused by: java.lang.NoSuchMethodException: sendMessage [class android.view.View]
04-10 01:37:00.520: E/AndroidRuntime(1650): at java.lang.Class.getConstructorOrMethod(Class.java:472)
04-10 01:37:00.520: E/AndroidRuntime(1650): at java.lang.Class.getMethod(Class.java:857)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.view.View$1.onClick(View.java:3803)
04-10 01:37:00.520: E/AndroidRuntime(1650): ... 11 more
04-10 01:37:04.820: I/Process(1650): Sending signal. PID: 1650 SIG: 9
I've tried to look at other questions answered similar to mine, but I can't find an answer that seems to help my situation. Can anyone help?
I was struggling with a similar error following the android tutorial
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myfirstapp, PID: 18300
java.lang.IllegalStateException: Could not find method sendMessage (MainActivity)(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5197)
at android.view.View$PerformClick.run(View.java:20909)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5944)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
but in my case the WYSIWYG editor (IntelliJ w/Android Studio plugin) ended up populating the onClick property in activity_main.xml with some extra junk about MainActivity:
android:onClick="sendMessage (MainActivity)"
So I deleted " (MainActivity)" and it stopped crashing. I see this is different from your problem as your xml file already seems correct with android:onClick="sendMessage". But wanted to add it here for any others struggling with what I saw. This was the closest post to the issue I was seeing. I'm just getting started and this was killing me, so hope it helps someone else.
The problem is the onClick property in one of your Button tags:
android:onClick="sendMessage" />
Just make sure you have a method sendMessage(View) in your Activity.
It is because you need to implement implements android.view.View.OnClickListener in your class, therefore add the correct imports i.e. import android.view.View.