Fragments : unable to start activity Component Info - android

I'm really confused on what I've done with my code.
It's just a simple app using Fragments but I get below error :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sgrumo.sunshineonyourmind/com.example.sgrumo.sunshineonyourmind.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
This is my MainActivity class :
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState==null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new MainActivityFragment()).commit();
}
}
Activity-main XML :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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/container"
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:ignore="MergeRootFrame"
tools:context="com.example.sgrumo.sunshineonyourmind.MainActivity">
I just wanted a Fragment inside my activity main, what's wrong with my code?
This is the Fragment class i use :
public class MainActivityFragment extends Fragment {
public MainActivityFragment() {}
String[] forecastArray = {
"Today - Sunny - 31/20",
"Tomorrow - Sunny - 42,30",
"Wednesday - Rainy - 20,15",
"Thursday - FINIMONDO - 35,20"
};
ArrayList<String> list = new ArrayList<>(Arrays.asList(forecastArray));
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),R.layout.list_item_forecast,R.id.list_item_forecast_textview,list);
ListView lw = (ListView)rootView.findViewById(R.id.listview_forecast);
lw.setAdapter(adapter);
return rootView;
}
And its XML :
<FrameLayout 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.sgrumo.sunshineonyourmind.MainActivityFragment"
tools:showIn="#layout/activity_main">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listview_forecast"
android:layout_gravity="center" />

Please change the code as following in your onCreateView method
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
You missed to add false. Thanks.

Use getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new MainActivityFragment()).commit() instead of getSupportFragmentManager().beginTransaction()
.add(R.id.container, new MainActivityFragment()).commit()

Related

Android calendar (caldroid) create 2 calendar views

Hi I'm using Caldroid calendar, and I use initialization code from official site only I use getFragmentManager() instead getSupportFragmentManager() because I use code in class which extend from Fragment not Activity. MysTab is fragment of TabbedActivity. In result I have 2 calendars and I do not know why. Any ideas?
Screenshot
public class MysTab extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.logs_tab, container, false);
CaldroidFragment caldroidFragment = new CaldroidFragment();
Bundle args = new Bundle();
Calendar cal = Calendar.getInstance();
args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
caldroidFragment.setArguments(args);
FragmentTransaction t = getFragmentManager().beginTransaction();
t.replace(R.id.caldroidCal, caldroidFragment);
t.commit();
return rootView;
}
}
Layout:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<fragment
android:id="#+id/caldroidCal"
android:name="com.roomorama.caldroid.CaldroidFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
</LinearLayout>
You need to decide whether you want to add a calendar dynamic or use what a layout gives to you.
If you want to add a calendar dynamic then your code should look like this:
public class MysTab extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.logs_tab, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
CaldroidFragment caldroidFragment = new CaldroidFragment();
Bundle args = new Bundle();
Calendar cal = Calendar.getInstance();
args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
caldroidFragment.setArguments(args);
FragmentTransaction t = getChildFragmentManager().beginTransaction();
t.replace(R.id.caldroidCal, caldroidFragment);
t.commit();
}
}
And in the same time your layout should be look like this:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/caldroidCal" />
</LinearLayout>
If you want to add a calendar fragment static then you just need to create a fragment and inflate your layout. No need to create a fragment manually:
public class MysTab extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.logs_tab, container, false);
}
}
And layout looks like that:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.roomorama.caldroid.CaldroidFragment"
android:id="#+id/caldroidCal" />
</LinearLayout>

ListView not displaying

I am trying to display a listview of items at the launch of my application.But at the launch of the application no listview is displayed but only a blank screen.Below are the codes:
MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public static class PlaceholderFragment extends Fragment
{
ArrayAdapter<String> mForecastAdapter;
public PlaceholderFragment()
{}
#Override
public View onCreateView(LayoutInflater inflator, ViewGroup v, Bundle savedInstanceState)
{
View rootView = inflator.inflate(R.layout.fragment_main, v);
String[] forecastArray= {
"Today - Sunny - 88/63",
"Monday - foggy - 67/20",
"Tuesday - Sunny - 88/63"
};
List<String> weekforecast= new ArrayList<String>(Arrays.asList(forecastArray));
mForecastAdapter= new ArrayAdapter<String>(
getActivity(),
R.layout.list_item_forecast,
R.id.list_item_forecast_textview,
weekforecast);
ListView listview =(ListView)rootView.findViewById(R.id.listview_forecast);
listview.setAdapter(mForecastAdapter);
return rootView;
}
}
}
activity_main.xml
<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"
tools:context="com.example.sunshineapp.MainActivity"
tools:ignore="MergeRootFrame" />
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="64dp"
android:paddingRight="64dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
tools:context="com.example.sunshineapp.MainActivity$PlaceholderFragment" >
<ListView
android:id="#+id/listview_forecast"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
list_item_forecast.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:id="#+id/list_item_forecast_textview"/>
Can Someone please guide me with the issue? Thanks in advance!
Edit:
As suggested by #Sanjeet I had forgotten to attach the fragment to the activity.So I attached it as follows in the onCreate method of activity:
if(savedInstanceState == null)
{
android.app.FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container, new PlaceholderFragment());
fragmentTransaction.commit();
}
But now I am getting the following in the logcat:
08-03 11:55:37.365: E/AndroidRuntime(20088): FATAL EXCEPTION: main
08-03 11:55:37.365: E/AndroidRuntime(20088): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sunshineapp/com.example.sunshineapp.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
08-03 11:55:37.365: E/AndroidRuntime(20088): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
08-03 11:55:37.365: E/AndroidRuntime(20088): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
08-03 11:55:37.365: E/AndroidRuntime(20088): at android.app.ActivityThread.access$600 (ActivityThread.java:162)
And the application stops..Please help!
You should use
View rootView = inflator.inflate(R.layout.fragment_main, v,false);
instead of
View rootView = inflator.inflate(R.layout.fragment_main, v);
To me somehow helped to update the adapter simply:
ListView listview =(ListView)rootView.findViewById(R.id.listview_forecast);
listview.setAdapter(mForecastAdapter);
mForecastAdapter.notifyDataSetChanged();
return rootView;
it seems to me that your mForecastAdapter array is empty. try to add some values inside for testing first.
Try changing
List<String> weekforecast= new ArrayList<String (Arrays.asList(forecastArray));
to
ArrayList<String> weekforecast= new ArrayList<String>(Arrays.asList(forecastArray));
Try changing
View rootView = inflator.inflate(R.layout.fragment_main, v);
to
View rootView = inflator.inflate(R.layout.fragment_main, v,false);
For more info about false in last parameter. You should check this answer.
Hope this helps.

IllegalArgumentException:No view found for id

I get the following exception:
java.lang.IllegalArgumentException:
No view found for id 0x7f04001a (com.whatever:layout/fragment_main)
Looking at similar questions on SO:
One answer suggests using getChildFragmentManager() instead of getSupportFragmentManager().
This is not an option since MainActivity is not a Fragment.
Another answer suggests that fragment_main is not a child of activity_main, but I think I have that setup in activity_main.xml with tools:layout="#layout/fragment_main
Another suggests that setContent() has not been called, but I have that in onCreate()
What am I missing?
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onStart() {
super.onStart();
// create fragment immediately
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.layout.fragment_main, new MainActivityFragment());
fragmentTransaction.commit();
boolean wereThereWereAnyPendingTransactions = fragmentManager.executePendingTransactions();
}
}
MainActivityFragment.java
public class MainActivityFragment extends Fragment implements Observer<String> {
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.whatever.MainActivityFragment"
tools:layout="#layout/fragment_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_main_table"
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=".MainActivityFragment">
<TextView
android:text="#string/fragment_main_xml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
The answer with respect to code:
actvity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_frame_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
</FrameLayout>
MainActivity.java:
...
fragmentTransaction.add(R.id.activity_frame_layout, new MainActivityFragment());
...
If you embed a Fragment inside of an Activity XML layout, you do not create it dynamically. The system does it for you. So there is no need to create the transaction and commit it, the Fragment will be created and added when you called setContentView().
Note that the tools:layout= line does not do anything at runtime. It is strictly for the tools to better link layouts and components together.

Binary XML file line #1: Error inflating class fragment

I'm trying to follow the Udacity Developing Android Apps course. I got to Lesson 3.03. Here, we are instructed to launch a new Activity (DetailActivity) from ForecastFragment when clicking an item in a list.
However, I keep getting an error when attempting to launch the DetailActivity. Binary XML file line #1: Error inflating class fragment, where it's caused by DetailActivity.java in it's onCreate() method.
I've already searched around, but none of the fixes suggested for this issue helped me at all.
The specific error is:
Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class
com.myapp.sunshine.app.DetailActivityFragment that is not a Fragment
DetailActivity.java
public class DetailActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail); //failing here
}
}
DetailActivityFragment.java
public class DetailActivityFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_detail, container, false);
}
}
activity_detail.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.myapp.sunshine.app.DetailActivityFragment"
tools:layout="#layout/fragment_detail" android:layout_width="match_parent"
android:layout_height="match_parent" />
I should mention that I'm not using support libraries--as I'm targeting SDK 21+, Lollipop only.
This isn't the only fragment we've created in the course either. The other fragment we created looks identical to this one.
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.myapp.sunshine.app.ForecastFragment"
tools:layout="#layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
And for good measure, I'm including the ForecastFragment class. I've omitted unnecessary parts of code.
ForecastFragment.java
public class ForecastFragment extends Fragment {
private ArrayAdapter<String> mForecaseAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String[] forecastArray = {
"Today - Sunny - 88/63",
"Tomorrow - Foggy - 74/58",
"Sunday - Rainy - 65/59",
"Monday - Cloudy - 72/67"
};
final List<String> weekForecast = new ArrayList<>(Arrays.asList(forecastArray));
mForecaseAdapter = new ArrayAdapter<>(
getActivity(),
R.layout.list_item_forecast,
R.id.list_item_forecast_textview,
weekForecast);
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
final ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
listView.setAdapter(mForecaseAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String forecast = mForecaseAdapter.getItem(position);
Intent detailActivityIntent = new Intent(getActivity(), DetailActivity.class)
.putExtra(Intent.EXTRA_TEXT, forecast);
startActivity(detailActivityIntent);
}
});
return rootView;
}
}
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: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=".DetailActivityFragment">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
which is nearly identical to
fragment_main.xml
<FrameLayout 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=".MainActivityFragment">
<ListView
android:id="#+id/listview_forecast"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
</FrameLayout>
You should be using FragmentActivity instead of Activity
public class DetailActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail); //failing here
}
}

Framelayout in Fragment Tab

I'm trying to get a frame layout in my app. I have two tabs, each tab is a fragment.
In one tab (listview) I want that two Seekbars appear when a button on the action bar is pressed.I know that I have to use Framelayout but not how to apply it? Please give me some hints how to get this done
Thanks
I think may be you can reference my sample follow ,it use android.support.v4 library.
At first, write a layout file for your activity like this :
<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:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<android.support.v4.app.FragmentTabHost
android:id="#+id/frag_tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/content_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
then in your MainActivity code, you can do like this :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
initializeTabHost();
}
/**
* 初始化TabHost
*/
private void initializeTabHost() {
tabHost.setup(this, getSupportFragmentManager(), R.id.content_layout);
TabSpec tabSound = tabHost.newTabSpec(TAB_SOUND).setIndicator(
getString(R.string.settings_sound));
TabSpec tabAdvanced = tabHost.newTabSpec(TAB_ADVANCED).setIndicator(
getString(R.string.settings_advanced));
TabSpec tabLocal = tabHost.newTabSpec(TAB_LOCAL).setIndicator(
getString(R.string.settings_local));
tabHost.addTab(tabSound, SoundFragment.class, null);
tabHost.addTab(tabAdvanced, AdvancedFragment.class, null);
tabHost.addTab(tabLocal, LocalFragment.class, null);
}
and make this class extends FragmentActivity.
then write a class extends Fragment, override the onCreateView method :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list, container, false);
return view;
}
Hope it can help you.

Categories

Resources