I am writing a simple app that displays a fragment in the main activity. When I run the app, it displays two instances of the fragment on the screen. I have included the code below. By the way, when I remove FT.commit(); from the main activity class, only one fragment instance is displayed on the screen as it should. What is the problem here?
thanks.
Fragment layout XML file
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Fragment Class
package com.example.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class fragmentone extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragoner, container, false);
}
}
Main activity layout XML file:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<fragment
android:name="com.example.fragment.fragmentone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragone"
tools:layout="#layout/fragoner">
</fragment>
</RelativeLayout>
Main Activity Class
package com.example.fragment;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager manager = getFragmentManager();
FragmentTransaction FT = manager.beginTransaction();
fragmentone Fone = new fragmentone();
FT.add(R.id.fragone,Fone);
FT.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.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
What is the problem here?
If you use the <fragment> tag, do not also use a FragmentTransaction to add the same fragment. Those are two separate techniques and should not be mixed.
Simply get rid of every line from onCreate() except the call to super.onCreate() and the setContentView() call, and stick with your static <fragment>.
Either remove the from your activity's layout view or stop using commit. You can use a as a container for your fragment. Add an id to your FrameLayout than use:
FragmentManger manager=getFragmentManager();
Fragment fragment=manager.findFragmentById(R.id.frameContainer);
Related
Because the class is somewhat old, and the IDE changed so much, i'm struggling a bit trying to do the same steps of the class.
I have created my Intent and i can send it by clicking on the ListView Item. Then it switches activities from the MainActivity to the DetailActivity. Yet when the DetailActivity is displayed (using DetailActivityFragment) the toolbar disappears. I compared XML's and made some improvements but now the toolbar isn't populated like the previous toolbar. The activity also "fills" the notification bar (the bar that extends to the drawer).
Question is, what am I doing wrong and what can i do to replicate the MainActivity toolbar to the new DetailActivity?
MainActivity.java
package com.example.diomonogatarilaptop.sunshine;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Something", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_refresh) {
MainActivityFragment.FetchWeatherTask weatherTask = new MainActivityFragment.FetchWeatherTask();
weatherTask.execute("Buraca");
return true;
}
return super.onOptionsItemSelected(item);
}
}
DetailActivity.java:
package com.example.diomonogatarilaptop.sunshine;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
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 DetailActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container_detail, new DetailActivityFragment())
.commit();
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_detail);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.detail, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class DetailActivityFragment extends Fragment {
TextView textView;
public DetailActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Intent intent =getActivity().getIntent();
View rootView =inflater.inflate(R.layout.fragment_detail, container, false);
if(intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) {
String forecastStr = intent.getStringExtra(Intent.EXTRA_TEXT);
textView= (TextView) rootView.findViewById((R.id.detail_text));
textView.setText(forecastStr);
}
return rootView;
}
}
}
Activity_detail.xml:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/container_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.sunshine.app.DetailActivity"
tools:ignore="MergeRootFrame">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_detail"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_detail" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
Content_detail.xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment"
android:name="com.example.diomonogatarilaptop.sunshine.DetailActivity$DetailActivityFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:layout="#layout/fragment_detail"
/>
Fragment_detail.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.example.diomonogatarilaptop.sunshine.DetailActivity$DetailActivityFragment"
tools:showIn="#layout/activity_detail">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Text"
android:id="#+id/detail_text"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Some ScreenCaptures from debugging on my mobile:
MainActivity
DetailActivity Where the bug happens
When I go into the view hierarchy it show RelativeLayout as the Viewgroup for the TextView and AnalogClock. So where did the fragment view go? Isn't the fragment contain the RelativeLayout, TextView, and AnalogClock in fragment_main.xml since it is the one that is inflated in MainActivity.java? Also, where did the TextView with "TestTest" as its text go?
Here's my code:
MainActivity:
package com.example.micha.solo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#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.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
MainActivityFragment:
package com.example.micha.solo;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A placeholder fragment containing a simple view.
*/
public class MainActivityFragment extends Fragment {
public MainActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
}
activity_main.xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/fragment"
android:name="com.example.micha.solo.MainActivityFragment" tools:layout="#layout/fragment_main"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView android:text="TestTest" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</fragment>
and fragment_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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
tools:showIn="#layout/activity_main">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<AnalogClock
android:layout_width="100dp"
android:layout_height="100dp" />
</RelativeLayout>
XML layout file
<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.gaurav.googlemap.HomeMap" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<fragment android:name="com.google.android.gms.maps.MapFragment"
android:id="#+id/map"
android:layout_height="match_parent"
android:layout_width="match_parent">
</fragment>
</RelativeLayout>
Java file
package com.beproject.ourway;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
public class HomeMap extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_map);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home_map, 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);
}
}
As I searched this question before also, I have done following things to solve
1.Imported following classes
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
2.Extended FragmentActivity class
Still I am getting this error in logcat
Error inflating class fragment
Is there any other way to solve this? Thanks.
Given that you have used 'support fragment' you need to replace MapFragment with SupportMapFragment. Doing this should fix it.
Use:
class="com.google.android.gms.maps.SupportMapFragment"
instead of :
android:name="com.google.android.gms.maps.MapFragment"
so your xml must be like:
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
I'm having an issue that I can't quite figure out. I've got all three of my buttons to take me to another activity, but the problem is that they all lead me to the activity that I only want my first (Easy) button to do.
fragment_main_menu.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.app.whosesloganisthat.MainMenu$PlaceholderFragment" >
<requestFocus />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView1"
android:layout_centerHorizontal="true"
android:text="#string/welcometowhosesloganisthat"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/buttonHard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttonHard"
android:layout_alignParentBottom="true"
android:layout_marginBottom="46dp"
android:onClick="sendMessage"
android:text="#string/hard" />
<Button
android:id="#+id/buttonEasy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_marginTop="48dp"
android:onClick="sendMessage"
android:text="#string/easy" />
<Button
android:id="#+id/buttonIntermediate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttonEasy"
android:layout_centerVertical="true"
android:onClick="sendMessage"
android:text="#string/intermediate" />
</RelativeLayout>
MainMenu.java:
package com.app.whosesloganisthat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.os.Build;
public class MainMenu extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.app.whosesloganisthat.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main_menu); //xml file name which contains button
}
#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, 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);
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, EasyLevelInfo.class);
startActivity(intent);
// Do something in response to button
}
/**
* 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_menu,
container, false);
return rootView;
}
}
EasyLevelInfo.java:
package com.app.whosesloganisthat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;
public class EasyLevelInfo extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainMenu.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
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.easy_level_info, 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_easy_level_info,
container, false);
return rootView;
}
}
fragment_easy_level_info.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.app.whosesloganisthat.EasyLevelInfo$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:text="#string/youhaveselectedeasy"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Thanks everyone!
Look in your fragment_main_menu.xml
layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView1"
android:layout_centerHorizontal="true"
android:text="#string/welcometowhosesloganisthat"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/buttonHard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttonHard"
android:layout_alignParentBottom="true"
android:layout_marginBottom="46dp"
android:onClick="sendMessage"
android:text="#string/hard" />
<Button
android:id="#+id/buttonEasy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_marginTop="48dp"
android:onClick="sendMessage"
android:text="#string/easy" />
<Button
android:id="#+id/buttonIntermediate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttonEasy"
android:layout_centerVertical="true"
android:onClick="sendMessage"
android:text="#string/intermediate" />
You have three buttons buttonHard, buttonEasy , buttonIntermediate . And these three buttons have attribute android:onClick = "sendMessage" This means if one of these buttons is clicked do sendMessage method . Lets look on that method.
public void sendMessage(View view) {
Intent intent = new Intent(this, EasyLevelInfo.class);
startActivity(intent);
// Do something in response to button
}
This method starts Activity EasyLevelInfo . So if one of three buttons is clicked it will call this sendMessage method and open EasyLevelInfo Activity .
was testing som things in Android Studios and ran in to this error:
FATAL EXCEPTION: main
Process: com.meisolsson.app, PID: 25610
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.meisolsson.app/com.meisolsson.app.LoginActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f070053 (com.meisolsson.app:id/container) for fragment PlaceholderFragment{4284e750 #0 id=0x7f070053}
Here is my LoginActivity.java
package com.meisolsson.app;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.view.Window;
import android.widget.Button;
public class LoginActivity extends ActionBarActivity {
private Button loginButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_login);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
this.setContentView(R.layout.fragment_login);
this.loginButton = (Button)this.findViewById(R.id.button);
this.loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
#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);
}
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_login, container, false);
return rootView;
}
}
}
And my fragment_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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".LoginActivity$PlaceholderFragment"
android:focusable="true"
android:background="#fcff00">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:hint="#string/user"
android:padding="10dp"
android:background="#ffffff"
android:layout_above="#+id/editText2"
android:layout_alignLeft="#+id/editText2"
android:layout_alignRight="#+id/editText2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editText2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:hint="#string/pass"
android:background="#ffffff"
android:padding="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/login"
android:id="#+id/button"
style="#style/AppTheme"
android:background="#ff5900"
android:shadowColor="#000000"
android:layout_below="#+id/editText2"
android:layout_alignLeft="#+id/editText"
android:layout_alignRight="#+id/editText2"
android:clickable="true"/>
</RelativeLayout>
The problem is the way you're doing this. For the Activity, you don't set the Fragment's view, you set a separate view that has a spot to put a Fragment. Depending on how you did your PlaceHolderFragment, the following ought to get a working app.
First add this file:
activity_main.xml
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Then change this stuff in your Activity:
LoginActivity.java
/* Same stuff*/
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
//This is the important change right here!
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
//The stuff that you had handling buttons should go in your
//PlaceHolderFragment class's onCreateView() method
}
I hope this helps. Good Luck!