InflateException Binary XML file at line #7: Error inflating class fragment - java

I've been doing some research of this problem but no solution has yet to work for me. I've added empty constructors in the fragment classes and tried with different imports of fragments, but nothing seems to work. I hope you guys can help me!
My Activity:
package com.example.com.example.android.rssfeed;
import android.app.Activity;
import android.os.Bundle;
public class RssfeedActivity extends Activity implements MyListFragment.OnItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rssfeed);
}
public void onRssItemSelected(String link) {
DetailFragment fragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.detailFragment);
if (fragment != null && fragment.isInLayout()) {
fragment.setText(link);
}//end if
}//end method onRssItemSelected
}//end class
My fragment:
package com.example.com.example.android.rssfeed;
import android.app.Activity;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MyListFragment extends Fragment {
public MyListFragment() {
}
private OnItemSelectedListener listener;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_rsslist_overview, container, false);
Button button = (Button) view.findViewById(R.id.btn1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
updateDetail();
}//end onClick
});
return view;
}//end onCreateView
public interface OnItemSelectedListener {
public void onRssItemSelected(String link);
}//end onItemSelectedListener
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof OnItemSelectedListener) {
listener = (OnItemSelectedListener) activity;
} else {
throw new ClassCastException(activity.toString()
+ "Must implement MyListFragment.OnItemSelectedListener");
}//end else
}//end onAttach
//May also be triggered from the activity
public void updateDetail() {
//Create fake data
String newTime = String.valueOf(System.currentTimeMillis());
// Send data to Activity
listener.onRssItemSelected(newTime);
}//end method updateDetail
}//end class
My fragment XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:id="#+id/listFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
class="com.example.android.rssfeed.MyListFragment"
android:layout_marginTop="?android:attr/actionBarSize"></fragment>
<fragment
android:id="#+id/detailFragment"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
class="com.example.android.rssfeed.DetailFragment"></fragment>
</LinearLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.com.example.android.rssfeed"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.com.example.android.rssfeed.RssfeedActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Button XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press to Update">
</button>
</LinearLayout>
LogCat error:
05-17 10:35:01.864: E/AndroidRuntime(6384): FATAL EXCEPTION: main
05-17 10:35:01.864: E/AndroidRuntime(6384): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.com.example.android.rssfeed/com.example.com.example.android.rssfeed.RssfeedActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread.access$700(ActivityThread.java:150)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.os.Looper.loop(Looper.java:137)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread.main(ActivityThread.java:5279)
05-17 10:35:01.864: E/AndroidRuntime(6384): at java.lang.reflect.Method.invokeNative(Native Method)
05-17 10:35:01.864: E/AndroidRuntime(6384): at java.lang.reflect.Method.invoke(Method.java:511)
05-17 10:35:01.864: E/AndroidRuntime(6384): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
05-17 10:35:01.864: E/AndroidRuntime(6384): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
05-17 10:35:01.864: E/AndroidRuntime(6384): at dalvik.system.NativeStart.main(Native Method)
05-17 10:35:01.864: E/AndroidRuntime(6384): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
05-17 10:35:01.864: E/AndroidRuntime(6384): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:342)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Activity.setContentView(Activity.java:1928)
05-17 10:35:01.864: E/AndroidRuntime(6384): at com.example.com.example.android.rssfeed.RssfeedActivity.onCreate(RssfeedActivity.java:11)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Activity.performCreate(Activity.java:5267)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
05-17 10:35:01.864: E/AndroidRuntime(6384): ... 11 more
05-17 10:35:01.864: E/AndroidRuntime(6384): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.android.rssfeed.MyListFragment: make sure class name exists, is public, and has an empty constructor that is public
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Fragment.instantiate(Fragment.java:592)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Fragment.instantiate(Fragment.java:560)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Activity.onCreateView(Activity.java:4862)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
05-17 10:35:01.864: E/AndroidRuntime(6384): ... 21 more
05-17 10:35:01.864: E/AndroidRuntime(6384): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.android.rssfeed.MyListFragment" on path: /data/app/com.example.com.example.android.rssfeed-1.apk
05-17 10:35:01.864: E/AndroidRuntime(6384): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
05-17 10:35:01.864: E/AndroidRuntime(6384): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-17 10:35:01.864: E/AndroidRuntime(6384): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Fragment.instantiate(Fragment.java:582)
05-17 10:35:01.864: E/AndroidRuntime(6384): ... 24 more

Caused by: java.lang.ClassNotFoundException: Didn't find class
"com.example.android.rssfeed.MyListFragment" on path:
/data/app/com.example.com.example.android.rssfeed-1.apk
Change this
class="com.example.android.rssfeed.MyListFragment"
to
class="com.example.com.example.android.rssfeed.MyListFragment"
cause the packagename is package com.example.com.example.android.rssfeed.
Similarly
class="com.example.com.example.android.rssfeed.DetailFragment"
if the package name is the same
Change this
<button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press to Update">
To
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press to Update" />

Change
"class= com.example.android.rssfeed.MyListFragment"
to
"com.example.com.example.android.rssfeed.MyListFragment"
And your problem will be solved out

Add this line in fragment
class="com.google.android.gms.maps.SupportMapFragment"

Related

Error Inflating Activity from Fragment: InflateException

I have a fragment, and I want to click on a button in the fragment to open a corresponding Activity that also hosts a fragment.
I am trying to test how the Activity appears (after button click) and am receiving what appears to be an inflation error. Here is the first fragment containing the button to view the next fragment:
HomePollsFragment.Java
mLatestTestButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent latestActivity = new Intent(getActivity().getApplicationContext(), LatestActivity.class);
startActivity(latestActivity);
}
});
And then here is the code for the activity that is opened on button click:
public class LatestActivity extends AppCompatActivity implements LatestFragment.OnFragmentInteractionListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_latest);
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.latest_fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
LatestFragment latestFragment = new LatestFragment();
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
latestFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.latest_fragment_container, latestFragment).commit();
}
}
#Override
public void onFragmentInteraction(Uri uri) {
}
}
The error I am receiving is below and appears to be related to the following line:
setContentView(R.layout.activity_latest);
Error:
06-27 22:35:47.787 4252-4252/com.sourcey.materialloginexample E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sourcey.materialloginexample/com.troychuinard.fanpolls.LatestActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.troychuinard.fanpolls.LatestActivity.onCreate(LatestActivity.java:14)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
at android.app.ActivityThread.access$600(ActivityThread.java:141) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:5103) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException: name == null
at java.lang.VMClassLoader.findLoadedClass(Native Method)
at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:491)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.support.v4.app.Fragment.isSupportFragmentClass(Fragment.java:454)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2252)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:356)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
at com.troychuinard.fanpolls.LatestActivity.onCreate(LatestActivity.java:14) 
at android.app.Activity.performCreate(Activity.java:5133) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
at android.app.ActivityThread.access$600(ActivityThread.java:141) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:5103) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 
activity_latest.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">
<fragment
android:id="#+id/latest_fragment_container"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />
The error is telling you that you have no name attribute on the <fragment> element specifying its class.
NullPointerException: name == null
However, it looks like you mean to load the Fragment yourself, so you don't want a <fragment> element. Instead, you want a ViewGroup that will hold the Fragment after the dynamic FragmentTransaction. Change your <fragment> to a <FrameLayout>.
That because you have a wrong on your activity_latest.xml. You tried to add your LatestFragment to Fragment. You can add fragment into ViewGroup like FrameLayout. Change your code like here.
<?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">
<FrameLayout
android:id="#+id/latest_fragment_container"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />

Error in Fragment Activity in Android

I am trying a fragment app as per a tutorial and getting following error:
06-28 04:21:47.118: D/AndroidRuntime(1491): Shutting down VM
06-28 04:21:47.128: W/dalvikvm(1491): threadid=1: thread exiting with uncaught exception (group=0xb4af7b90)
06-28 04:21:47.158: E/AndroidRuntime(1491): FATAL EXCEPTION: main
06-28 04:21:47.158: E/AndroidRuntime(1491): Process: com.example.fragmentapp, PID: 1491
06-28 04:21:47.158: E/AndroidRuntime(1491): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentapp/com.example.fragmentapp.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.access$700(ActivityThread.java:135)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.os.Handler.dispatchMessage(Handler.java:102)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.os.Looper.loop(Looper.java:137)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.main(ActivityThread.java:4998)
06-28 04:21:47.158: E/AndroidRuntime(1491): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 04:21:47.158: E/AndroidRuntime(1491): at java.lang.reflect.Method.invoke(Method.java:515)
06-28 04:21:47.158: E/AndroidRuntime(1491): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
06-28 04:21:47.158: E/AndroidRuntime(1491): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
06-28 04:21:47.158: E/AndroidRuntime(1491): at dalvik.system.NativeStart.main(Native Method)
06-28 04:21:47.158: E/AndroidRuntime(1491): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
06-28 04:21:47.158: E/AndroidRuntime(1491): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Activity.setContentView(Activity.java:1928)
06-28 04:21:47.158: E/AndroidRuntime(1491): at com.example.fragmentapp.MainActivity.onCreate(MainActivity.java:13)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Activity.performCreate(Activity.java:5243)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
06-28 04:21:47.158: E/AndroidRuntime(1491): ... 11 more
06-28 04:21:47.158: E/AndroidRuntime(1491): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.pavan.fragmentdemo.MyListFragment: make sure class name exists, is public, and has an empty constructor that is public
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Fragment.instantiate(Fragment.java:597)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Fragment.instantiate(Fragment.java:561)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Activity.onCreateView(Activity.java:4777)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
06-28 04:21:47.158: E/AndroidRuntime(1491): ... 21 more
06-28 04:21:47.158: E/AndroidRuntime(1491): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.pavan.fragmentdemo.MyListFragment" on path: DexPathList[[zip file "/data/app/com.example.fragmentapp-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.fragmentapp-2, /system/lib]]
06-28 04:21:47.158: E/AndroidRuntime(1491): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
06-28 04:21:47.158: E/AndroidRuntime(1491): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
06-28 04:21:47.158: E/AndroidRuntime(1491): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
06-28 04:21:47.158: E/AndroidRuntime(1491): at android.app.Fragment.instantiate(Fragment.java:583)
06-28 04:21:47.158: E/AndroidRuntime(1491): ... 24 more
The code is as follows:
Main Activity.java
package com.example.fragmentapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity implements
ListFragment.Communicator {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void Message(String OS_Name) {
DetailFragment detailfragment = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.detail_Fragment);
if (detailfragment != null && detailfragment.isInLayout()) {
detailfragment.setText(OS_Name);
}
else {
Intent intent = new Intent(getApplicationContext(),
DetailActivity.class);
Bundle extras = new Bundle();
extras.putString(DetailActivity.os_name, OS_Name);
intent.putExtras(extras);
startActivity(intent);
}
}
}
Code of DetailActivity.java
package com.example.fragmentapp;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
public class DetailActivity extends Activity {
public static String os_name = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
finish();
return;
}
setContentView(R.layout.detail_activity);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String name = extras.getString(os_name);
DetailFragment detailFragment = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.detailFragment);
detailFragment.setText(name);
}
}
}
Code of DetailFragment.java
package com.example.fragmentapp;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DetailFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detail_fragment, container, false);
return view;
}
// we call this method when button from listfragment is clicked
public void setText(String item) {
TextView view = (TextView) getView().findViewById(R.id.display_tv);
view.setText(item);
}
}
Code of ListFragment.java
package com.example.fragmentapp;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class ListFragment extends Fragment implements OnClickListener {
private Communicator communicator;
Button android_btn, ios_btn, windows_btn;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof Communicator) {
communicator = (Communicator) activity;
}
else {
throw new
ClassCastException(activity.toString()
+ " must implemenet MyListFragment.Communicator");
}
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view =
inflater.inflate(R.layout.list_fragment, container, false);
// Initialize Views android_btn = (Button)
view.findViewById(R.id.android_btn_id);
ios_btn = (Button)
view.findViewById(R.id.ios_btn_id);
windows_btn = (Button)
view.findViewById(R.id.windows_btn_id);
// set on click Listeners for buttons
android_btn.setOnClickListener(this);
ios_btn.setOnClickListener(this);
windows_btn.setOnClickListener(this);
return view;
}
//Create Interface
public interface Communicator {
public void Message(String OS_Name);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.android_btn_id:
updateFragment("Android");
break;
case R.id.ios_btn_id:
updateFragment("IOS");
break;
case R.id.windows_btn_id:
updateFragment("Windows");
break;
}
}
private void updateFragment(String OS_Name) {
communicator.Message(OS_Name);
}
}
In Layout Folder
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:id="#+id/list_Fragment"
android:layout_width="0sp"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.pavan.fragmentdemo.MyListFragment" >
</fragment>
<fragment
android:id="#+id/detail_Fragment"
android:layout_width="0sp"
android:layout_height="match_parent"
android:layout_weight="2"
class="com.pavan.fragmentdemo.DetailFragment" >
</fragment>
</LinearLayout>
Code of detail_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFF99"
android:orientation="vertical"
android:padding="20dp" >
<TextView
android:id="#+id/display_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="40sp" />
</LinearLayout>
code of list_fragment.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CCFF99"
android:orientation="vertical"
android:padding="5dp" >
<Button
android:id="#+id/android_btn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android" />
<Button
android:id="#+id/ios_btn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IOS" />
<Button
android:id="#+id/windows_btn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Windows" />
</LinearLayout>
In layout-port
code activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/list_Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.pavan.fragmentdemo.MyListFragment" >
</fragment>
</LinearLayout>
Code of detail_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/detailFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.pavan.fragmentdemo.DetailFragment" />
</LinearLayout>
You have different names of fragments:
in xml com.pavan.fragmentdemo.MyListFragment
in java com.example.fragmentapp.MyListFragment
fix package names

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

I created an app by following the tutorial at http://www.techotopia.com/index.php/Using_Fragments_in_Android_-_A_Worked_Example,
but I have a error.
LogCat:
05-02 08:16:22.044: D/dalvikvm(1846): Late-enabling CheckJNI
05-02 08:16:22.080: D/AndroidRuntime(1846): Shutting down VM
05-02 08:16:22.080: W/dalvikvm(1846): threadid=1: thread exiting with uncaught exception (group=0xa4d81b20)
05-02 08:16:22.096: E/AndroidRuntime(1846): FATAL EXCEPTION: main
05-02 08:16:22.096: E/AndroidRuntime(1846): Process: com.example.myfragmentexample, PID: 1846
05-02 08:16:22.096: E/AndroidRuntime(1846): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfragmentexample/com.example.myfragmentexample.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.os.Handler.dispatchMessage(Handler.java:102)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.os.Looper.loop(Looper.java:136)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-02 08:16:22.096: E/AndroidRuntime(1846): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 08:16:22.096: E/AndroidRuntime(1846): at java.lang.reflect.Method.invoke(Method.java:515)
05-02 08:16:22.096: E/AndroidRuntime(1846): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-02 08:16:22.096: E/AndroidRuntime(1846): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-02 08:16:22.096: E/AndroidRuntime(1846): at dalvik.system.NativeStart.main(Native Method)
05-02 08:16:22.096: E/AndroidRuntime(1846): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
05-02 08:16:22.096: E/AndroidRuntime(1846): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.Activity.setContentView(Activity.java:1929)
05-02 08:16:22.096: E/AndroidRuntime(1846): at com.example.myfragmentexample.MainActivity.onCreate(MainActivity.java:12)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.Activity.performCreate(Activity.java:5231)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-02 08:16:22.096: E/AndroidRuntime(1846): ... 11 more
05-02 08:16:22.096: E/AndroidRuntime(1846): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.fragmentexample.ToolbarFragment: make sure class name exists, is public, and has an empty constructor that is public
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.Fragment.instantiate(Fragment.java:597)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.Fragment.instantiate(Fragment.java:561)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.Activity.onCreateView(Activity.java:4778)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
05-02 08:16:22.096: E/AndroidRuntime(1846): ... 21 more
05-02 08:16:22.096: E/AndroidRuntime(1846): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.fragmentexample.ToolbarFragment" on path: DexPathList[[zip file "/data/app/com.example.myfragmentexample-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.myfragmentexample-1, /system/lib]]
05-02 08:16:22.096: E/AndroidRuntime(1846): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
05-02 08:16:22.096: E/AndroidRuntime(1846): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
05-02 08:16:22.096: E/AndroidRuntime(1846): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
05-02 08:16:22.096: E/AndroidRuntime(1846): at android.app.Fragment.instantiate(Fragment.java:583)
05-02 08:16:22.096: E/AndroidRuntime(1846): ... 24 more
MainActivity.java:
package com.example.myfragmentexample;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity implements
ToolbarFragment.ToolbarListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtononClick(int fontsize, String text) {
TextFragment textfragment = (TextFragment) getSupportFragmentManager()
.findFragmentById(R.id.text_fragment);
textfragment.zmienWlasciwosci(fontsize, text);
}
}
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"
tools:context=".MainActivity" >
<fragment
android:id="#+id/toolbar_fragment"
android:name="com.example.fragmentexample.ToolbarFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
tools:layout="#layout/toolbar_fragment" />
<fragment
android:id="#+id/text_fragment"
android:name="com.example.fragmentexample.TextFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
tools:layout="#layout/text_fragment" />
</RelativeLayout>
ToolbarFragment.java:
package com.example.myfragmentexample;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class ToolbarFragment extends Fragment implements
OnSeekBarChangeListener {
private static int seekvalue = 10;
private static EditText edittext;
ToolbarListener activityCallback;
public interface ToolbarListener {
public void onButtononClick(int position, String text);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
activityCallback = (ToolbarListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement ToolbarListener");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.toolbar_fragment, container,
false);
edittext = (EditText) view.findViewById(R.id.editText);
SeekBar seekbar = (SeekBar) view.findViewById(R.id.seekBar);
seekbar.setOnSeekBarChangeListener(this);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
buttonClicked(v);
}
});
return view;
}
public void buttonClicked(View view) {
activityCallback.onButtononClick(seekvalue, edittext.getText()
.toString());
}
#Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
seekvalue = arg1;
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
}
toolbar_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="text">
<requestFocus />
</EditText>
<SeekBar
android:id="#+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText"
android:layout_marginTop="14dp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:layout_below="#+id/seekBar"
android:text="#string/button_text" />
</RelativeLayout>
TextFragment.java:
package com.example.myfragmentexample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class TextFragment extends Fragment {
private static TextView textview;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.text_fragment, container, false);
textview = (TextView) view.findViewById(R.id.textView1);
return view;
}
public void zmienWlasciwosci (int fontsize, String text){
textview.setTextSize(fontsize);
textview.setText(text);
}
}
text_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/text_label"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfragmentexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.myfragmentexample.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I read a lot of posts about this error, but I couldn't find anything wrong in my code. I extended android.support.v4.app.FragmentActivity.
Caused by: java.lang.ClassNotFoundException: Didn't find class
"com.example.fragmentexample.ToolbarFragment" on path:
DexPathList[[zip file
"/data/app/com.example.myfragmentexample-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.myfragmentexample-1,
/system/lib]]
This
android:name="com.example.fragmentexample.ToolbarFragment"
Must be
android:name="com.example.myfragmentexample.ToolbarFragment"
cause package name for ToolbarFragment.java is
package com.example.myfragmentexample;
Similarly for TextFragment
android:name="com.example.myfragmentexample.TextFragment"
Try adding correct layouts at correct place like ex. in my case I was putting colors.xml in values-w820p then it was giving runtime exception of classtype. I changed the folder from this to simple value and... taddda!! it worked! :)
I am also new to Android and encountered the same error today. I could not find a solution that worked for me but one thing was clear to me that this issue occurs when android is not able to resolve a mandatory attribute of one of the elements in layout xml.
So I checked my layout thoroughly and found that by mistake I created an ID
<item type="id" name="login"/>
in ids.xml. This Id was conflicting with attribute
android:imeActionId="#+id/login"
of EditText element.
Hope this helps someone.
In my case Google Maps were not correctly initialized. A message in log console printed about it. I had to add
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
to <application> inside AndroidManifest.

The application has stopped unexpectedly . Error-caused by: Java.lang.NullPointerException

main.java
package com.learnactivities;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Main.this, Second.class));
}
});
}
}
activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android: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=".Main" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button1" />
</RelativeLayout>
Second.java
package com.learnactivities;
import android.app.Activity;
import android.os.Bundle;
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}
second.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=".Second" >
<TextView
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="15dp"
android:text="#string/nd" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.learnactivities"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.learnactivities.Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".second" />
</application>
</manifest>
I m getting following errors. I can see that error caused by java.lang.NullPointerException. I did my best searching here and there but could not get to the solution.Plz, anyone help me sort this problem out?
08-25 19:08:44.442: D/AndroidRuntime(276): Shutting down VM
08-25 19:08:44.442: W/dalvikvm(276): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-25 19:08:44.472: E/AndroidRuntime(276): FATAL EXCEPTION: main
08-25 19:08:44.472: E/AndroidRuntime(276): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.learnactivities/com.learnactivities.Main}: java.lang.NullPointerException
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.os.Handler.dispatchMessage(Handler.java:99)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.os.Looper.loop(Looper.java:123)
08-25 19:08:44.472: E/AndroidRuntime(276): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-25 19:08:44.472: E/AndroidRuntime(276): at java.lang.reflect.Method.invokeNative(Native Method)
08-25 19:08:44.472: E/AndroidRuntime(276): at java.lang.reflect.Method.invoke(Method.java:521)
08-25 19:08:44.472: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-25 19:08:44.472: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-25 19:08:44.472: E/AndroidRuntime(276): at dalvik.system.NativeStart.main(Native Method)
08-25 19:08:44.472: E/AndroidRuntime(276): Caused by: java.lang.NullPointerException
08-25 19:08:44.472: E/AndroidRuntime(276): at com.learnactivities.Main.onCreate(Main.java:19)
08-25 19:08:44.472: E/AndroidRuntime(276): at `android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)`
`08-25 19:08:44.472: E/AndroidRuntime(276): at` `android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)`
08-25 19:08:44.472: E/AndroidRuntime(276): ... 11 more
R.id.button1 is declared inside second.xml, but you are looking for it inside activity.xml. So in Main, when you do
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
findViewById is returning null
Try this in your main class..
TextView b = (TextView ) findViewById(R.id.textView1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Main.this, Second.class));
}
});
and also in manifest change like below line
<activity android:name=".Second" />

Unfortunately app has stopped working

I am new to android application development. I was doing this tutorial app.It's a very simple one. It adds one and subtracts one from the counter.When I run it in the emulator ,it says "Unfortunately tutorial has stopped working." There are no errors in the code. The API level is 17. Please help me out.
Code for java:
public class Startingpoint extends Activity {
int counter=0;
Button add,subtract;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startingpoint);
add = (Button) findViewById(R.id.bAdd);
subtract= (Button) findViewById(R.id.bSubtract);
display= (Button) findViewById(R.id.text);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter++;
display.setText("The total is " + counter);
}
});
subtract.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter--;
display.setText("The total is " + counter);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.startingpoint, menu);
return true;
}
}
Layout code in xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:textSize="35dp"
android:layout_gravity="center"
android:gravity="center"/>
<Button android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Add One"
android:layout_gravity="center"
android:textSize="25dp"
android:id="#+id/bAdd"/>
<Button android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Subtract One"
android:layout_gravity="center"
android:textSize="25dp"
android:id="#+id/bSubtract"/>
</LinearLayout>
Here is the logcat :
03-02 02:45:10.730: D/AndroidRuntime(780): Shutting down VM
03-02 02:45:10.730: W/dalvikvm(780): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-02 02:45:10.750: E/AndroidRuntime(780): FATAL EXCEPTION: main
03-02 02:45:10.750: E/AndroidRuntime(780): java.lang.RuntimeException: Unable to start activity ComponentInfo{tutorial.example.tutorial/tutorial.example.tutorial.Startingpoint}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.os.Looper.loop(Looper.java:137)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-02 02:45:10.750: E/AndroidRuntime(780): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 02:45:10.750: E/AndroidRuntime(780): at java.lang.reflect.Method.invoke(Method.java:511)
03-02 02:45:10.750: E/AndroidRuntime(780): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-02 02:45:10.750: E/AndroidRuntime(780): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-02 02:45:10.750: E/AndroidRuntime(780): at dalvik.system.NativeStart.main(Native Method)
03-02 02:45:10.750: E/AndroidRuntime(780): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
03-02 02:45:10.750: E/AndroidRuntime(780): at tutorial.example.tutorial.Startingpoint.onCreate(Startingpoint.java:22)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.Activity.performCreate(Activity.java:5104)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-02 02:45:10.750: E/AndroidRuntime(780): ... 11 more
display= (Button) findViewById(R.id.text);
should be
display= (TextView) findViewById(R.id.text);
Since display is supposed to reference a TextView instance, but you're explictly casting into a Button, and these are not compatible types.
As you got the answer from A-C this time, but for next time in android application development a important suggestion:
Always see the logcat for error, and see the "Caused by:" tag, It specifies what was the cause of the problem with sufficient detail, Also see the line no that caused that error.
And try to find what can be wrong with that line of code.
For example: in your logcat it is showing-
Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
at tutorial.example.tutorial.Startingpoint.onCreate(Startingpoint.java:22)
So you can try to read the log, and you will understand that in your file Startingpoint.java at line 22 which is located in onCreate method the error is android.widget.TextView cannot be cast to android.widget.Button. So you can easily remove your errors without any help.
Hope that helps not only you current problem but prevented your future time and efforts.

Categories