Android : Error inflating class fragment - java

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"/>

Related

Fragment as ViewGroup?

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>

How to navigate activitie's layout through tabs android java

In my android application I have three layout's activity and three tabs and below is my activity_1:
package com.example.testapp;
import java.io.ByteArrayOutputStream;
import android.support.v4.*;
import android.support.v7.app.ActionBarActivity;
import android.app.TabActivity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Base64;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
public class MainActivity extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt_display = (TextView)findViewById(R.id.txt_display);
btn_s = (Button)findViewById(R.id.btn_go);
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent_one;
intent_one = new Intent().setClass(this, MainActivity.class);
spec = tabHost.newTabSpec("tabOne");
spec.setContent(intent_one);
spec.setIndicator("Tab One");
tabHost.addTab(spec);
}
#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);
}
}
below is layout of this activity:
<?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">
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</TabHost>
<RelativeLayout
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.testapp.MainActivity" >
<TextView
android:id="#+id/txt_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="96dp"
android:text="#string/hello_world" />
</RelativeLayout>
</RelativeLayout>
I am doing the same code in the other two activities, and I want to change layout's activity through tabs and i am using api level-16 and v7 but getting exception
The type TabActivity is deprecated
How can I resolve this issue, kindly suggest me waiting for reply.
Thanks
This is done by the view pager and fragment.
Here
is the link to develope the swipable tabs. you'll get a demo code for that.
According to the documentation
New applications should use Fragments instead of this class; to
continue to run on older devices, you can use the v4 support library
which provides a version of the Fragment API that is compatible down
to DONUT.
So use fragments instead.

Fragment application displaying 2 instances of the same fragment in Android Studio

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);

Image not getting displayed in emulator

I am implementing a simple page similar to any other app, a 'header image' and content below.
I've placed a wedhead.png file manually into all the drawable folders that I want to display.
The layout I wish to implement displays correctly on the graphical editor, but not on the emulator. The emulator comes out blank. Emulator is ARM CPU based.
Emulator screen -
Here's my code
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: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.wwmk1.MainActivity" >
<ImageView
android:id="#+id/img1"
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_marginBottom="19dp"
android:contentDescription="header"
android:fadingEdge="vertical"
android:src="#drawable/wedhead"
android:visibility="visible" />
</RelativeLayout>
The Main activity java file
package com.example.wwmk1;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView iv = (ImageView)findViewById(R.id.img1);
}
#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);
}
}
What place did I go wrong?
Please help. Thanks

Eclipse, Creating Android Projects, won't import javax.swing

I normally create android apps using either eclipse or NetBeans but when I try to create a messagebox on button press, Eclipse in this case won't import javax.swing
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="136dp"
android:text="Button" />
</RelativeLayout>
MainActivity.java:
package com.example.understanding;
import android.os.Bundle;
import android.app.Activity;
import javax.swing;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
private void button1_ButtonActionPerformed(View v) {
JOptionPane.showMessageDialog(null, "Enter Account Details");
}
#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;
}
}
javax.swing does not exist on Android and is not part of the Android SDK.

Categories

Resources