Error Inflating Activity from Fragment: InflateException - java

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

Related

YouTube Android Player API - ExceptionInInitializerError

Note: I'm using the YouTube Android Player API
Expected behavior:
The activity remains in portrait mode when the video is not fullscreen (Enforced by AndroidManifest ok).
The activity is set to landscape orientation when the video enters fullscreen mode (ok).
The activity returns to portrait orientation when the user exits fullscreen mode (ExceptionInInitializerError occurs here).
See the problem in action here
YouTubeFailureRecoveryActivity.java (This is included in the library under sample/src/com/examples/youtubeapidemo)
Main Activity
package test.testapp;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;
public class MainActivity extends YouTubeFailureRecoveryActivity implements YouTubePlayer.OnFullscreenListener {
static final String API_KEY = "PLACE YOUTUBE DATA API KEY HERE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(API_KEY, this);
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
youTubePlayer.setOnFullscreenListener(this);
youTubePlayer.setFullscreenControlFlags(YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI);
if(!wasRestored){
youTubePlayer.cueVideo("wKJ9KzGQq0w");
}
}
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}
#Override
public void onFullscreen(boolean isFullscreen) {
if(isFullscreen){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}else if(!isFullscreen){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
}
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="test.testapp.MainActivity">
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtube_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.google.android.youtube.player.YouTubePlayerView>
</LinearLayout>
Stack trace
06-10 09:47:09.205 2646-2646/test.testapp E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.ExceptionInInitializerError
at android.support.v7.widget.RecyclerView.onSaveInstanceState(SourceFile:201)
at android.view.View.dispatchSaveInstanceState(View.java:11839)
at android.view.ViewGroup.dispatchFreezeSelfOnly(ViewGroup.java:2576)
at android.support.v7.widget.RecyclerView.dispatchSaveInstanceState(SourceFile:220)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562)
at android.view.View.saveHierarchyState(View.java:11822)
at com.android.internal.policy.impl.PhoneWindow.saveHierarchyState(PhoneWindow.java:1566)
at android.app.Activity.onSaveInstanceState(Activity.java:1188)
at com.google.android.youtube.player.YouTubeBaseActivity.onSaveInstanceState(Unknown Source)
at android.app.Activity.performSaveInstanceState(Activity.java:1137)
at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1215)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3486)
at android.app.ActivityThread.access$700(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: rt
at rs.<clinit>(SourceFile:17)
at android.support.v7.widget.RecyclerView.onSaveInstanceState(SourceFile:201) 
at android.view.View.dispatchSaveInstanceState(View.java:11839) 
at android.view.ViewGroup.dispatchFreezeSelfOnly(ViewGroup.java:2576) 
at android.support.v7.widget.RecyclerView.dispatchSaveInstanceState(SourceFile:220) 
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562) 
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562) 
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562) 
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562) 
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562) 
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562) 
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2562) 
at android.view.View.saveHierarchyState(View.java:11822) 
at com.android.internal.policy.impl.PhoneWindow.saveHierarchyState(PhoneWindow.java:1566) 
at android.app.Activity.onSaveInstanceState(Activity.java:1188) 
at com.google.android.youtube.player.YouTubeBaseActivity.onSaveInstanceState(Unknown Source) 
at android.app.Activity.performSaveInstanceState(Activity.java:1137) 
at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1215) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3486) 
at android.app.ActivityThread.access$700(ActivityThread.java:130) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 
Looks like the application is crashing when restarting because of a configuration change
https://developer.android.com/guide/topics/resources/runtime-changes.html
You can disable the automatic activity recreation for orientation changes, and handle it yourself.
To do that, add
android:configChanges="orientation|screenLayout|screenSize"
With orientation alone it probably works, but the other ones I think were triggered also on some devices.
You can then override onConfigurationChanged() to do stuff on an orientation change if you need it, like showing / hiding views, etc.. as Android will not recreate your layouts automatically (that's what it does when restarts the activity on an orientation change)
So you can do:
#Override
public void onConfigurationChanged(final Configuration newConfiguration) {
Log.wtf(
"Orientation",
newConfiguration.orientation == Configuration.ORIENTATION_PORTRAIT
? "portrait"
: "landscape"
);
}

Inflation exception error from android

Hello i'm struggling with these error for past of couple of hours to no avail. I am trying to pass data from my activity to the fragment but not succeeding in doing so. My guess it's complaining about my <fragment> in activity_second.xml file. My guess might be wrong. Has anyone encountered this error? But i don't know how to resolve this error.Would some please guide in me what i did wrong. Below is my code:
first_fragment.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/fragmentPic" >
<TextView
android:id="#+id/textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/users_FirstName"
android:textSize="#dimen/font_size"
android:layout_gravity="center_horizontal"/>
<com.facebook.login.widget.ProfilePictureView
android:id="#+id/profilePic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
>
</com.facebook.login.widget.ProfilePictureView>
</LinearLayout>
FirstFragment.java:
public class FirstFragment extends Fragment {
private ProfilePictureView profilePictureView;
private TextView textView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// return super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.first_fragment, container, false);
Bundle bundle = new Bundle();
bundle = this.getArguments();
String profileId = bundle.getString("profileId"); //complains here and returns null
TextView textView = (TextView)rootView.findViewById(R.id.textName);
textView.setText(bundle.getString("names"));
return rootView;
}
}
activity_second.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"
android:background="#color/backgroundColor"
>
<include android:id="#+id/toolBar"
layout="#layout/toolbar" />
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawer">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="#layout/first_fragment"
class="edu.sjsu.cmpe277.termproject.Fragments.FirstFragment"
android:id="#+id/fragment1"
/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
secondActivity.java:
public class secondActivity extends AppCompatActivity {
private Intent intent;
private Toolbar toolbar;
private String firstName, profileId;
private FirstFragment firstFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
intent = getIntent();
firstName = intent.getStringExtra("firstName");
profileId = intent.getStringExtra("Id");
toolbar = (Toolbar)findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
toolbar.setLogo(R.drawable.ic_menu_image);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
firstFragment = new FirstFragment();
Bundle bundle = new Bundle();
bundle.putString("names", firstName);
bundle.putString("profileId", profileId);
firstFragment.setArguments(bundle);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.drawer, firstFragment,"newFrag").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_second, 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.
switch(item.getItemId()) {
case android.R.id.home:
Intent homeIntent = new Intent(this, secondActivity.class );
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And this error below:
29 01:50:11.402 13536-13536/edu.sjsu.cmpe277.termproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: edu.sjsu.cmpe277.termproject, PID: 13536
java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.sjsu.cmpe277.termproject/edu.sjsu.cmpe277.termproject.secondActivity}: android.view.InflateException: Binary XML file line #82: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: android.view.InflateException: Binary XML file line #82: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
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:255)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at edu.sjsu.cmpe277.termproject.secondActivity.onCreate(secondActivity.java:43)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
            at android.app.ActivityThread.access$900(ActivityThread.java:177)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5942)
            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:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at edu.sjsu.cmpe277.termproject.Fragments.FirstFragment.onCreateView(FirstFragment.java:44)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:995)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1185)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1287)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2243)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:278)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:78)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:740)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
            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:255)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
            at edu.sjsu.cmpe277.termproject.secondActivity.onCreate(secondActivity.java:43)
            at android.app.Activity.performCreate(Activity.java:6289)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
            at android.app.ActivityThread.access$900(ActivityThread.java:177)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5942)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
Get rid of
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="#layout/first_fragment"
class="edu.sjsu.cmpe277.termproject.Fragments.FirstFragment"
android:id="#+id/fragment1"
/>
and use only the explicitly transaction you are using in your Activity. You can't pass a Bundle, declaring a Fragment in the layout, and try to accessing it, is making your app crash
In your activity_second.xml file, change:
class="edu.sjsu.cmpe277.termproject.Fragments.FirstFragment"
to
android:name="edu.sjsu.cmpe277.termproject.Fragments.FirstFragment"
That's the correct prefix to use.

Android app crashes after rotating from LANDSCAPE to PORTRAIT

I'm building my first Android app and I've consulted quite some tutorials and stackoverflow posts, but I can't seem to find the proper fix for this problem.
I've been following YouTube tutorials from TheNewBoston and Derek Banas, as well as some written tutorials.
My app runs as it should when starting the app (being in Portrait mode). When I swap it to landscape, it still works fine. However when I rotate it back to portrait, the app crashes with the following error:
04-12 23:42:12.331 28004-28004/nl.avans.mbd2.android.gideon.van.david.multipanefragment E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: nl.avans.mbd2.android.gideon.van.david.multipanefragment, PID: 28004
java.lang.RuntimeException: Unable to start activity ComponentInfo{nl.avans.mbd2.android.gideon.van.david.multipanefragment/nl.avans.mbd2.android.gideon.van.david.multipanefragment.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4054)
at android.app.ActivityThread.access$1000(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1314)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:240)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
at nl.avans.mbd2.android.gideon.van.david.multipanefragment.MainActivity.onCreate(MainActivity.java:18)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4054)
            at android.app.ActivityThread.access$1000(ActivityThread.java:175)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1314)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Fragment nl.avans.mbd2.android.gideon.van.david.multipanefragment.MainPanelFragment did not create a view.
at android.app.Activity.onCreateView(Activity.java:5020)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:240)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at nl.avans.mbd2.android.gideon.van.david.multipanefragment.MainActivity.onCreate(MainActivity.java:18)
            at android.app.Activity.performCreate(Activity.java:5451)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4054)
            at android.app.ActivityThread.access$1000(ActivityThread.java:175)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1314)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
The actual error being:
java.lang.RuntimeException: Unable to start activity ComponentInfo{nl.avans.mbd2.android.gideon.van.david.multipanefragment/nl.avans.mbd2.android.gideon.van.david.multipanefragment.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
Caused by: java.lang.IllegalStateException: Fragment nl.avans.mbd2.android.gideon.van.david.multipanefragment.MainPanelFragment did not create a view.
My MainActivity looks like this:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int screenOrientation = getResources().getConfiguration().orientation;
if (screenOrientation == Configuration.ORIENTATION_PORTRAIT) {
hideSidePanel();
}
}
private void hideSidePanel() {
View detailPane = findViewById(R.id.detail_panel);
if(detailPane.getVisibility() == View.VISIBLE) {
detailPane.setVisibility(View.GONE);
}
}
}
MainPanelFragment:
public class MainPanelFragment extends ListFragment {
boolean orientation;
int currentListViewItem;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> connectArrayToListView = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_activated_1, API.dummyData);
setListAdapter(connectArrayToListView);
orientation = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
if (savedInstanceState != null) {
currentListViewItem = savedInstanceState.getInt("curChoice", 0);
}
if (orientation) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
showDetails(currentListViewItem);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragment_main_panel, container, false);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("curChoice", currentListViewItem);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
showDetails(position);
// super.onListItemClick(l, v, position, id);
}
void showDetails(int kaart) {
currentListViewItem = kaart;
if (orientation) {
getListView().setItemChecked(kaart, true);
DetailPanelFragment details = (DetailPanelFragment) getFragmentManager().findFragmentById(R.id.detail_panel);
if (details == null || details.getShowIndex() == -1) {
details = DetailPanelFragment.newInstance(kaart);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.main_panel, details);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
} else {
Intent intent = new Intent();
intent.setClass(getActivity(), DetailsActivity.class);
intent.putExtra("index", kaart);
startActivity(intent);
}
}
}
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:animateLayoutChanges="true"
tools:context=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:id="#+id/main_panel"
android:layout_alignParentRight="true"
class="nl.avans.mbd2.android.gideon.van.david.multipanefragment.MainPanelFragment"/>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="#+id/detail_panel"
android:layout_alignParentLeft="true"
class="nl.avans.mbd2.android.gideon.van.david.multipanefragment.DetailPanelFragment"/>
</LinearLayout>
fragment_detail_panel.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:background="#android:color/holo_blue_bright"
android:layout_gravity="top|center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Detail panel"
android:textColor="#android:color/white"
android:textStyle="bold"
android:id="#+id/kaartenListViewDetail"
/>
</LinearLayout>
fragment_main_panel.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:background="#android:color/holo_red_dark"
android:layout_gravity="top|center"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/list">
</ListView>
</LinearLayout>
I also have a folder in /res named layout-land. It contains an activity_main.xml file which is exactly identical to the one posted above.
I've seen similiar questions like this one but it seems that this situation is not (enough) the same to find a valid answer.

Android NumberPicker NullPointer why?

i am try to implement a numberPicker to select minute values.
But i am getting a NullPointer Exception at this line:
minutePicker = (NumberPicker) findViewById(R.id.minuten_picker);
Following Code:
public class MainActivity extends Activity {
NumberPicker minutePicker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Auswahl Minuten zum starten / Stoppen aller
minutePicker = new NumberPicker(MainActivity.this);
minutePicker = (NumberPicker) findViewById(R.id.minuten_picker);
minutePicker.setMaxValue(30);
minutePicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
abschaltzeit = minutePicker.getValue();
}
});
minutePicker.setValue(0);
minutePicker.setWrapSelectorWheel(false);
}
}
XML:
<NumberPicker
android:id="#+id/minuten_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="6"
android:layout_column="0"
android:paddingLeft="20dp" />
Log:
09-25 11:00:09.749 10687-10687/de.carsten.awesome.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: de.carsten.awesome.app, PID: 10687
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.carsten.awesome.app/de.carsten.awesome.app.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at de.carsten.awesome.app.MainActivity.onCreate(MainActivity.java:83)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
minutePicker = new NumberPicker(MainActivity.this);
minutePicker = (NumberPicker) findViewById(R.id.minuten_picker);
You're creating a NumberPicker programmatically and then overwriting the reference with whatever findViewById() returns. It returns null if your activity_main layout does not contain a minuten_picker.
Choose only the other: either create it prorgrammatically or find it from a view hierarchy you inflated.
If you choose the programmatic way new NumberPicker(), remember to add it to some layout in your activity view hierarchy, e.g. with setContentView()
If you choose the inflation way, make sure you have the view in your XML layout file.
I'm guessing the NPE you're seeing is actually on the following line where you're trying to invoke a method on the minutePicker and it's null.
I apologize.
I moved the Code from the MainActivity in the creating Fragment and it works with rootView.findView.
Sorry but i am new with the Fragement Konzept.
Thanks a lot for your help !

calling fragment from activity throws null pointer exception- Android

I am trying to call a List fragment from fragment activity, I m getting null pointer exception.Here is my code,
public class SampleEvents extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample_events);
FragmentManager fm = getFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_content);
if (fragment == null) {
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_content, new EventsList());
ft.commit();
}
}
Layout of fragment activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/fragment_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Layout of the fragment event_list
<?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"
android:background="#drawable/background"
android:orientation="vertical" >
<TextView
android:id="#+id/empty_TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#0c2c0c"
android:text="No Announcements Found"
android:textStyle="bold"
android:textSize="20sp" />
<ListView
android:id="#+id/events_list"
android:padding="10dip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</RelativeLayout>
Fragment class
public class EventsList extends ListFragment implements
android.widget.AdapterView.OnItemClickListener{
View rootView;
static SharedPreferences sPrefs;
private NetWorkConnection nc;
private BaseAlerts alert;
private ListView events_listview;
private TextView emptyTV;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.events_list, container, false);
initUI();
uiListener();
return rootView;
}
Whats wrong with my code?Can someone help?
The log cat
06-10 19:22:08.248: E/AndroidRuntime(1009): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ie.minoritybussiness/com.ie.minoritybussiness.dashboard.SampleEvents}: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
06-10 19:22:08.248: E/AndroidRuntime(1009): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-10 19:22:08.248: E/AndroidRuntime(1009): at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-10 19:22:08.248: E/AndroidRuntime(1009): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-10 19:22:08.248: E/AndroidRuntime(1009): at android.os.Handler.dispatchMessage(Handler.java:99)
06-10 19:22:08.248: E/AndroidRuntime(1009): at android.os.Looper.loop(Looper.java:137)
06-10 19:22:08.248: E/AndroidRuntime(1009): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-10 19:22:08.248: E/AndroidRuntime(1009): at java.lang.reflect.Method.invokeNative(Native Method)
06-10 19:22:08.248: E/AndroidRuntime(1009): at java.lang.reflect.Method.invoke(Method.java:511)
06-10 19:22:08.248: E/AndroidRuntime(1009): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-10 19:22:08.248: E/AndroidRuntime(1009): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-10 19:22:08.248: E/AndroidRuntime(1009): at dalvik.system.NativeStart.main(Native Method)
06-10 19:22:08.248: E/AndroidRuntime(1009): Caused by: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
06-10 19:22:08.248: E/AndroidRuntime(1009): at android.app.ListFragment.ensureList(ListFragment.java:402)
06-10 19:22:08.248: E/AndroidRuntime(1009): at android.app.ListFragment.onViewCreated(ListFragment.java:203)
06-10 19:22:08.248: E/AndroidRuntime(1009): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:843)
06-10 19:22:08.248: E/AndroidRuntime(1009): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
Content has view with id attribute 'android.R.id.list' that is not a ListView class
That looks like your issue. There are 2 reasons I can think of for this to be occuring.
You are giving the android id to a non-list view. Check out your XML files for anywhere you are using #android:id/list as an id for a view. Rename it to something else.
You are using a ListFragment/ListActivity and are not defining a view with #android:id/list as the id. These classes require that identifier. See this Stack Overflow post for some more details.
Put:
FragmentManager fragmentManager = getSupportFragmentManager();
instead of:
FragmentManager fm = getFragmentManager();
Edit:
If you are not using support library than this is probably not the reason for exception. I will give you example how do i do it:
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = (ListFragment) fragmentManager
.findFragmentByTag("List");
if (fragment != null) {
fragmentManager.beginTransaction()
.add(R.id.fragment, fragment, "List").addToBackStack("List")
.commit();
}
So, i am not using findFragmentById. I am using findFragmentTag and when i replace (or add) fragment, i put a tag ("List"). ListFragment is custom fragment i created.

Categories