fragments class unable to start activity componentinfo - android

I wanted to learn Fragment. So started to learning from tutorialspoint.com.
The Example was:
This example will explain you how to create your own Fragments. Here
we will create two fragments and one of them will be used when device
is in landscape mode and another fragment will be used in case of
portrait mode.
When I runned application occurred this Exception:
04-08 10:57:20.135 19408-19408/net.motameni.apps.fragments D/AndroidRuntime﹕ Shutting down VM
04-08 10:57:20.137 19408-19408/net.motameni.apps.fragments E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: net.motameni.apps.fragments, PID: 19408
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.motameni.apps.fragments/net.motameni.apps.fragments.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
at net.motameni.apps.fragments.MainActivity.onCreate(MainActivity.java:17)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException
at java.lang.VMClassLoader.findLoadedClass(Native Method)
at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:499)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.support.v4.app.Fragment.isSupportFragmentClass(Fragment.java:454)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2123)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
at android.support.v7.app.ActionBarActivity.onCreateView(ActionBarActivity.java:547)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at net.motameni.apps.fragments.MainActivity.onCreate(MainActivity.java:17)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-08 12:49:45.078 26843-26843/net.motameni.apps.fragments I/art﹕ Late-enabling -Xcheck:jni
04-08 12:49:45.445 26843-26843/net.motameni.apps.fragments D/AndroidRuntime﹕ Shutting down VM
04-08 12:49:45.452 26843-26843/net.motameni.apps.fragments E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: net.motameni.apps.fragments, PID: 26843
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.motameni.apps.fragments/net.motameni.apps.fragments.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
at net.motameni.apps.fragments.MainActivity.onCreate(MainActivity.java:17)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.IllegalStateException: Fragment net.motameni.apps.fragments.Raw_Fragment did not create a view.
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2145)
at android.app.Activity.onCreateView(Activity.java:5282)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at net.motameni.apps.fragments.MainActivity.onCreate(MainActivity.java:17)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-08 12:49:50.902 26843-26843/net.motameni.apps.fragments I/Process﹕ Sending signal. PID: 26843 SIG: 9
04-08 13:20:18.838 30365-30365/net.motameni.apps.fragments I/art﹕ Late-enabling -Xcheck:jni
04-08 13:20:19.081 30365-30384/net.motameni.apps.fragments D/OpenGLRenderer﹕ Render dirty regions requested: true
04-08 13:20:19.091 30365-30365/net.motameni.apps.fragments D/Atlas﹕ Validating map...
04-08 13:20:19.138 30365-30384/net.motameni.apps.fragments I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/28/14, c33033c, Ia6306ec328
04-08 13:20:19.139 30365-30384/net.motameni.apps.fragments I/OpenGLRenderer﹕ Initialized EGL, version 1.4
04-08 13:20:19.159 30365-30384/net.motameni.apps.fragments D/OpenGLRenderer﹕ Enabling debug mode 0
04-08 13:20:41.066 31240-31260/net.motameni.apps.fragments D/OpenGLRenderer﹕ Render dirty regions requested: true
04-08 13:20:41.072 31240-31240/net.motameni.apps.fragments D/Atlas﹕ Validating map...
04-08 13:20:41.114 31240-31260/net.motameni.apps.fragments I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/28/14, c33033c, Ia6306ec328
04-08 13:20:41.115 31240-31260/net.motameni.apps.fragments I/OpenGLRenderer﹕ Initialized EGL, version 1.4
04-08 13:20:41.135 31240-31260/net.motameni.apps.fragments D/OpenGLRenderer﹕ Enabling debug mode 0
04-08 13:21:02.984 32157-32207/net.motameni.apps.fragments D/OpenGLRenderer﹕ Render dirty regions requested: true
04-08 13:21:02.992 32157-32157/net.motameni.apps.fragments D/Atlas﹕ Validating map...
04-08 13:21:03.031 32157-32207/net.motameni.apps.fragments I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/28/14, c33033c, Ia6306ec328
04-08 13:21:03.032 32157-32207/net.motameni.apps.fragments I/OpenGLRenderer﹕ Initialized EGL, version 1.4
04-08 13:21:03.053 32157-32207/net.motameni.apps.fragments D/OpenGLRenderer﹕ Enabling debug mode 0
Why?????
I think this is may related to API level (My application API level is 21).
Please see example link and help. Thanks.
MainActivity.java:
package net.motameni.apps.fragments;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Configuration config = getResources().getConfiguration();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
/**
* Check the device orientation and act accordingly
*/
if(config.orientation == Configuration.ORIENTATION_LANDSCAPE){
//Landscape mode of the device
LM_Fragment lm_fragment = new LM_Fragment();
fragmentTransaction.replace(android.R.id.content,lm_fragment);
}else{
//Portrait mode of the device
PM_Fragment pm_fragment = new PM_Fragment();
fragmentTransaction.replace(android.R.id.content,pm_fragment);
}
fragmentTransaction.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);
}
}
activity_main.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:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<fragment
android:name="net.motameni.apps.fragments"
android:id="#+id/lm_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>
<fragment
android:name="net.motameni.apps.fragments"
android:id="#+id/pm_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"/>
</LinearLayout>
lm_fragment.xml (same as pm_fragment.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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="landscape"
android:gravity="center"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
LM_Fragment.java (same as RM_Fragment):
package net.motameni.apps.fragments;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class LM_Fragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
* Inflate the layout for this fragment
*/
return inflater.inflate(R.layout.lm_fragment,container,false);
}
}

The tag fragment in your xml should be: Fragment, it uses
capital letters.

First, the name of fragment in activity_main.xml should be your fragment's absolute path.In this case, name should be net.motameni.apps.fragments.LM_Fragment
Second, in the line of fragmentTransaction.replace(android.R.id.content,pm_fragment);, R should be your-pkg-name.R but not android.R. The former is the R you generated while the latter is generated by Android SDK.
Third, if you want to add a pm_fragment, you need to change your activity_main.xml into this:
<?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:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<FrameLayout
android:id="#+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Meanwhile, you need change your code like this:
fragmentTransaction.replace(R.id.content,pm_fragment);
Most importantly, the R is your-pkg-name.R

here in your fragment XML :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
you have to add a context to it and change it to :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.blaw.blawblaw">
then in your Main XMl :
<fragment
android:name="net.motameni.apps.fragments"
android:id="#+id/lm_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/>
you don't have this fragment "net.motameni.apps.fragments" ! you should change it to "com.blaw.blawblaw" then that fragment show on your page !
and you just need one fragment in your XML , you add two , that's wrong
just add one and then handle fragment changing on your main class !
Best Regard !

Related

android.view.InflateException: Binary XML file line #2: Error inflating class andorid.support.v7.widget.Toolbar

Hi I trying material design thing but I got the above error. Here is my log cat:
Process: course.examples.healthcare_application, PID: 19653
java.lang.RuntimeException: Unable to start activity ComponentInfo{course.examples.healthcare_application/course.examples.healthcare_application.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class andorid.support.v7.widget.Toolbar
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
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:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class andorid.support.v7.widget.Toolbar
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:904)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at course.examples.healthcare_application.MainActivity.onCreate(MainActivity.java:15)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
            at android.app.ActivityThread.access$900(ActivityThread.java:177)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5951)
            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:1400)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: java.lang.ClassNotFoundException: Didn't find class "andorid.support.v7.widget.Toolbar" on path: DexPathList[[zip file "/data/app/course.examples.healthcare_application-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.view.LayoutInflater.createView(LayoutInflater.java:578)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
            at android.view.LayoutInflater.parseInclude(LayoutInflater.java:904)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
            at course.examples.healthcare_application.MainActivity.onCreate(MainActivity.java:15)
            at android.app.Activity.performCreate(Activity.java:6289)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
            at android.app.ActivityThread.access$900(ActivityThread.java:177)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5951)
            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:1400)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Suppressed: java.lang.ClassNotFoundException: andorid.support.v7.widget.Toolbar
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 24 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
app_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<andorid.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#DDD">
</andorid.support.v7.widget.Toolbar>
acitivity_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="course.examples.healthcare_application.MainActivity">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<TextView
android:layout_below="#+id/app_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
MainActivity
package course.examples.healthcare_application;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar= (Toolbar) findViewById(R.id.app_bar);
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.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);
}
}
Please help. I had looked at other posts but it doesn't solve my problem. And I am new to material design. Don't quite understand the error.
andorid.support.v7.widget.Toolbar
to
android.support.v7.widget.Toolbar
<?xml version="1.0" encoding="utf-8"?>
<andorid.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#DDD">
</andorid.support.v7.widget.Toolbar>
Misspelled android

android.view.InflateException: Binary XML file line #24: Error inflating class fragment

I have an activity which has 2 buttons. The fragment in my activity would change on button click. But I am facing issue as -- android.view.InflateException: Binary XML file line #24: Error inflating class fragment .
Below is my code-
activity_main.xml
<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="vertical"
tools:context=".MainActivity">
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"
android:text="Select Fragment 1" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"
android:text="Select Fragment 2" />
<fragment
android:id="#+id/fragment_place"
android:name="com.example.rahul.fragmentdemo.FragmentOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="#layout/fragment_one" />
</LinearLayout>
MainActivity.java
package com.example.rahul.fragmentdemo;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void selectFrag(View view) {
Fragment fr = null;
if (view == findViewById(R.id.button2)) {
fr = new FragmentTwo();
} else {
fr = new FragmentOne();
}
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
}
}
FragmentOne.java
package com.example.rahul.fragmentdemo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment {
public FragmentOne() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
Log.i("Fragment Demo", "Fragment_One onCreate()");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_one, container, false);
return view;
}
}
fragment_one.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" tools:context="com.example.rahul.fragmentdemo.FragmentOne">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="#+id/tvFragment_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is Fragment 1" />
</FrameLayout>
similar to FragmentOne.java and fragment_two.xml, my code has FragmentTwo.java and fragment_two.xml respectively.
I get exception as--
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rahul.fragmentdemo/com.example.rahul.fragmentdemo.MainActivity}: android.view.InflateException: Binary XML file line #24: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
at android.app.Activity.setContentView(Activity.java:2144)
at com.example.rahul.fragmentdemo.MainActivity.onCreate(MainActivity.java:18)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.support.v4.app.SuperNotCalledException: Fragment FragmentOne{3fdec5ae #0 id=0x7f0c0051} did not call through to super.onCreate()
at android.support.v4.app.Fragment.performCreate(Fragment.java:1768)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:917)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1116)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1218)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2170)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:300)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
            at android.app.Activity.setContentView(Activity.java:2144)
            at com.example.rahul.fragmentdemo.MainActivity.onCreate(MainActivity.java:18)
            at android.app.Activity.performCreate(Activity.java:5937)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
-- where MainActivity.java:18 refers to
setContentView(R.layout.activity_main);
As the error log says, there is missing super.onCreate in FragmentOne.java:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("Fragment Demo", "Fragment_One onCreate()");
}

android.app.Fragment$InstantiationException: Trying to instantiate a class ScannerFragment that is not a Fragment

I updated a few libraries in app.gradle like support library from 22.2.0 -> 23.0.0 and versions of few third party libraries, and now I'm getting the following runtime error in one particular activity:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shubhamkanodia.bookmybook/com.example.shubhamkanodia.bookmybook.AddBooksActivity_}: android.view.InflateException: Binary XML file line #155: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
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:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: android.view.InflateException: Binary XML file line #155: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:257)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.example.shubhamkanodia.bookmybook.AddBooksActivity_.setContentView(AddBooksActivity_.java:56)
at com.example.shubhamkanodia.bookmybook.AddBooksActivity.onCreate(AddBooksActivity.java:140)
at com.example.shubhamkanodia.bookmybook.AddBooksActivity_.onCreate(AddBooksActivity_.java:46)
at android.app.Activity.performCreate(Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
            at android.app.ActivityThread.access$800(ActivityThread.java:155)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5343)
            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:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.example.shubhamkanodia.bookmybook.Fragments.ScannerFragment that is not a Fragment
at android.app.Fragment.instantiate(Fragment.java:606)
at android.app.Fragment.instantiate(Fragment.java:582)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2110)
at android.app.Activity.onCreateView(Activity.java:5348)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:257)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
            at com.example.shubhamkanodia.bookmybook.AddBooksActivity_.setContentView(AddBooksActivity_.java:56)
            at com.example.shubhamkanodia.bookmybook.AddBooksActivity.onCreate(AddBooksActivity.java:140)
            at com.example.shubhamkanodia.bookmybook.AddBooksActivity_.onCreate(AddBooksActivity_.java:46)
            at android.app.Activity.performCreate(Activity.java:6010)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
            at android.app.ActivityThread.access$800(ActivityThread.java:155)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5343)
            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:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.ClassCastException
            at android.app.Fragment.instantiate(Fragment.java:606)
            at android.app.Fragment.instantiate(Fragment.java:582)
            at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2110)
            at android.app.Activity.onCreateView(Activity.java:5348)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:257)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
            at com.example.shubhamkanodia.bookmybook.AddBooksActivity_.setContentView(AddBooksActivity_.java:56)
            at com.example.shubhamkanodia.bookmybook.AddBooksActivity.onCreate(AddBooksActivity.java:140)
            at com.example.shubhamkanodia.bookmybook.AddBooksActivity_.onCreate(AddBooksActivity_.java:46)
            at android.app.Activity.performCreate(Activity.java:6010)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
            at android.app.ActivityThread.access$800(ActivityThread.java:155)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5343)
            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:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
In particular this line seems strange:
Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.example.shubhamkanodia.bookmybook.Fragments.ScannerFragment that is not a Fragment
( ScannerFragment is a fragment that implements dm77 barcode scanner which i updated from 1.7.2 to 1.8.2 )
Scanner Fragment:
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import me.dm7.barcodescanner.zbar.BarcodeFormat;
import me.dm7.barcodescanner.zbar.Result;
import me.dm7.barcodescanner.zbar.ZBarScannerView;
public class ScannerFragment extends Fragment implements MessageDialogFragment.MessageDialogListener,
ZBarScannerView.ResultHandler, FormatSelectorDialogFragment.FormatSelectorDialogListener,
CameraSelectorDialogFragment.CameraSelectorDialogListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
mScannerView = new ZBarScannerView(getActivity());
//stuff
return mScannerView;
}
#Override
public void onActivityCreated(Bundle state){
super.onCreate(state);
//stuff
}
#Override
public void onCreate(Bundle state) {
super.onCreate(state);
setHasOptionsMenu(true);
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
//stuff
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
//stuff
}
#Override
public void onResume() {
super.onResume();
//stuff
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//stuff
}
#Override
public void handleResult(Result rawResult) {
//stuff
}
#Override
public void onPause() {
super.onPause();
//stuff
}
XML Snippet:
<FrameLayout
android:id="#+id/flScan"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/fScanner"
class="com.example.shubhamkanodia.bookmybook.Fragments.ScannerFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginTop="8dp"
android:background="#color/transparent"
android:padding="12dp">
<ImageButton
android:id="#+id/ibAutofocus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/ripple_light"
android:clickable="true"
android:contentDescription="Enable / Disable Autofocus"
android:longClickable="true"
android:padding="8dp"
android:src="#mipmap/ic_action_auto_focus_on" />
<ImageButton
android:id="#+id/ibFlashControl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/ibAutofocus"
android:background="#drawable/ripple_light"
android:clickable="true"
android:contentDescription="Enable / Disable Flash"
android:longClickable="true"
android:padding="8dp"
android:src="#mipmap/ic_action_image_flash_off" />
</RelativeLayout>
</FrameLayout>
Any help would be appreciated!
Try importing android.app.Fragment instead of android.support.v4.app.Fragment basically don't use support library if you don't need it 'or check that Activity in which you are inflating this fragment about : Whether you are using the FragmentManager and FragmentTransaction from support library only.

Android Studio: App doesn't start in emulator "Unfortunately ..app.. has stopped"

made a percentage calculator but it wont show up in emulator and before it starts u it just says "Unfortunately ..app.. has stopped"
Can someone advise me on what am doing wrong here?
Java code:
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void ButtonClick(View v)
{
EditText a = (EditText) findViewById(R.id.amount);
EditText p = (EditText) findViewById(R.id.percent);
TextView r = (TextView) findViewById(R.id.result);
double num1, num2, ans;
num1 = Double.parseDouble(a.getText().toString());
num2 = Double.parseDouble(p.getText().toString());
ans = 0;
if (v.getId() == R.id.bt1)
ans = num1 * num2 / 100;
if (v.getId() == R.id.bt2)
ans = (num1 * num2 / 100) + num1;
if (v.getId() == R.id.bt3)
ans = (num1 * num2 / 100) - num1;
r.setText(ans + "");
}
#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);
}
}
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"
android:background="#drawable/perccal2">
<ImageButton
android:layout_width="110dp"
android:layout_height="70dp"
android:id="#+id/bt1"
android:layout_marginTop="260dp"
android:background="#drawable/bt1"
android:onClick="ButtonClick" />
<ImageButton
android:layout_width="110dp"
android:layout_height="70dp"
android:id="#+id/bt2"
android:layout_marginTop="260dp"
android:layout_marginLeft="120dp"
android:background="#drawable/bt2"
android:onClick="ButtonClick" />
<ImageButton
android:layout_width="110dp"
android:layout_height="70dp"
android:id="#+id/bt3"
android:layout_marginTop="260dp"
android:layout_marginLeft="240dp"
android:background="#drawable/bt3"
android:onClick="ButtonClick" />
<EditText
android:layout_width="110dp"
android:layout_height="50dp"
android:id="#+id/amount"
android:layout_marginTop="50dp"
android:layout_marginLeft="210dp" />
<EditText
android:layout_width="80dp"
android:layout_height="50dp"
android:id="#+id/percent"
android:layout_marginTop="170dp"
android:layout_marginLeft="247dp"
/>
<TextView
android:layout_width="165dp"
android:layout_height="70dp"
android:id="#+id/result"
android:layout_marginTop="420dp"
android:layout_marginLeft="95dp"
/>
LogCat:
05-02 13:50:26.690 2300-2300/? E/art﹕ Throwing OutOfMemoryError
"Failed to allocate a 138883260 byte allocation with 1048576
free bytes and 63MB until OOM"
05-02 13:50:26.691 2300-2300/? D/skia﹕ ---
allocation failed for scaled bitmap
05-02 13:50:26.694 2300-2300/? D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
05-02 13:50:26.717 2300-2300/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.bluewhale.percentagecalculator, PID: 2300
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.bluewhale.percentagecalculator/
com.bluewhale.percentagecalculator.MainActivity}:
android.view.InflateException: Binary XML file line #1:
Error inflating class android.widget.RelativeLayout
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.view.InflateException: Binary XML file line #1: Error
inflating class android.widget.RelativeLayout
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at
com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView
(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView
(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView
(AppCompatActivity.java:106)
at com.bluewhale.percentagecalculator.MainActivity.onCreate
(MainActivity.java:17)
at android.app.Activity.performCreate(Activity.java:5937)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
           
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
             at android.app.ActivityThread.access$800(ActivityThread.java:144)
             at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
             at android.os.Handler.dispatchMessage(Handler.java:102)
             at android.os.Looper.loop(Looper.java:135)
             at android.app.ActivityThread.main(ActivityThread.java:5221)
             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:899)
             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance
(Constructor.java:288)
at
android.view.LayoutInflater.createView(LayoutInflater.java:607)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView
(PhoneLayoutInflater.java:55)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
            at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView
(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView
(AppCompatActivity.java:106)
             at com.bluewhale.percentagecalculator.MainActivity.onCreate
(MainActivity.java:17)
            at android.app.Activity.performCreate(Activity.java:5937)
            at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 138883260
byte allocation with 1048576 free bytes and 63MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at
android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at
android.graphics.drawable.Drawable.createFromResourceStream
(Drawable.java:973)
at
android.content.res.Resources.loadDrawableForCookie(Resources.java:2423)
at android.content.res.Resources.loadDrawable(Resources.java:2330)
at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
at android.view.View.<init>(View.java:3730)
at android.view.ViewGroup.<init>(ViewGroup.java:491)
at android.widget.RelativeLayout.<init>(RelativeLayout.java:248)
at android.widget.RelativeLayout.<init>(RelativeLayout.java:244)
at android.widget.RelativeLayout.<init>(RelativeLayout.java:240)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
            at android.view.LayoutInflater.createView(LayoutInflater.java:607)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView
(PhoneLayoutInflater.java:55)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
            at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView
(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView
(AppCompatActivity.java:106)
            at
com.bluewhale.percentagecalculator.MainActivity.onCreate
(MainActivity.java:17)
            at android.app.Activity.performCreate(Activity.java:5937)
            at android.app.Instrumentation.callActivityOnCreate
(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity
(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800
(ActivityThread.java:144)
            at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
05-02 13:50:26.728 1241-1289/? W/ActivityManager﹕
Force finishing activity com.bluewhale.percentagecalculator/.MainActivity
Thank you in advance!
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 138883260 byte allocation with 1048576 free bytes and 63MB until OOM
You are attempting to allocate 138883260 bytes (~132MB) for the image you are trying to use for the android:background of the RelativeLayout. You do not have anywhere near that amount of heap space. Please use a smaller image, or use something other than a bitmap for the RelativeLayout background, or do not use a background on this RelativeLayout.

Error inflating class RecyclerView

So my code simply makes a list of CardViews using RecyclerView. Upon running my code i kept getting a weird error claiming there was an error in my xml. After tinkering for a while i found out that in my layout file if i change <RecyclerView> to <android.support.v7.widget.RecyclerView> everything would work just fine. Why is this happening? My activity.
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
public class CardListActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card_list);
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.cardList);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
ArrayList<String> list = new ArrayList<>();
for(int i = 0; i < 20; i++) {list.add("Item " + i);}
CardListAdapter cardListAdapter = new CardListAdapter(list);
recyclerView.setAdapter(cardListAdapter);
}
}
My Adapter
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class CardListAdapter extends RecyclerView.Adapter<CardListAdapter.CardListViewHolder> {
private List<String> list;
public CardListAdapter(List<String> list) {
this.list = list;
}
#Override
public CardListViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_layout,viewGroup,false);
return new CardListViewHolder(v);
}
#Override
public void onBindViewHolder(CardListViewHolder cardListViewHolder, int i) {
String s = list.get(i);
cardListViewHolder.title.setText(s);
}
#Override
public int getItemCount() {
return list.size();
}
public static class CardListViewHolder extends RecyclerView.ViewHolder {
TextView title;
public CardListViewHolder(View itemView) {
super(itemView);
title = (TextView)itemView.findViewById(R.id.title);
}
}
}
My layout file, note, changing <RecyclerView> to <android.support.v7.widget.RecyclerView> fixes the error.
<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=".CardListActivity">
<RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/cardList">
</RecyclerView>
When i run with <RecyclerView> i get this error, .
`Process: com.liquidink.lollipopmaterialui, PID: 7317
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.liquidink.lollipopmaterialui/com.liquidink.lollipopmaterialui.CardListActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class RecyclerView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class RecyclerView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:757)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
at com.liquidink.lollipopmaterialui.CardListActivity.onCreate(CardListActivity.java:18)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.RecyclerView" on path: DexPathList[[zip file "/data/app/com.liquidink.lollipopmaterialui-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.view.LayoutInflater.createView(LayoutInflater.java:571)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:665)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:65)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at com.liquidink.lollipopmaterialui.CardListActivity.onCreate(CardListActivity.java:18)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Suppressed: java.lang.ClassNotFoundException: android.view.RecyclerView
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 26 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
`
You should add the RecyclerView in the XML in this way:
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/cardList">
</android.support.v7.widget.RecyclerView>
Hope it helps!
RecyclerView is not included in base Android framework, only widgets in base Android framework (like ListView, GridView etc) can be specified in layout without full namespace. RecyclerView is part of recyclerview-v7 support library.
I meet this problem today.
And solved it.
first step:keep the support-libs you used are same version
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
second step:you should add recyclerView to your proguard files
-keep class android.support.v7.widget.** {*;}
// I`ve just keep all widgets
Make sure to include before you add RecyclerView to your XML
compile 'com.android.support:recyclerview-v7:22.2.0'
compile 'com.android.support:appcompat-v7:22.2.0'
If you created RecyclerView in your XML before adding these dependencies, to make it work you should remove (comment) your recycler view, build project without it and then add it back. Otherwise it still shows Inflate exception in binary XML line #n.
Check reference in build.gradle
In your layout xml file be, write your RecyclerView component's name as a whole with its package like that android.support.v7.widget.RecyclerViewnot like RecyclerView
now ready to lunch. Good trails

Categories

Resources