Kostya Khuta
Kostya Khuta

Reputation: 1856

Error inflating class fragment in Android 2.1

I get the error

Unable to start activity ComponentInfo{de.androidbuch.activiti/de.androidbuch.activiti.task.Activity}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment

i read Error inflating class fragment in Android android, Error inflating class fragment but it does not work for me

 package com.example.fragments;

 import android.os.Bundle;
 import android.support.v4.app.FragmentActivity;
 import android.util.Log;

 public class MainActivity extends FragmentActivity {
String LOG_TAG = "myLogs";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    Log.d(LOG_TAG, "MainActivity onCreate");
}

protected void onStart() {
    super.onStart();
    Log.d(LOG_TAG, "MainActivity onStart");
}

protected void onResume() {
    super.onResume();
    Log.d(LOG_TAG, "MainActivity onResume");
}

protected void onPause() {
    super.onPause();
    Log.d(LOG_TAG, "MainActivity onPause");
}

protected void onStop() {
    super.onStop();
    Log.d(LOG_TAG, "MainActivity onStop");
}

protected void onDestroy() {
    super.onDestroy();
    Log.d(LOG_TAG, "MainActivity onDestroy");
}

}

Fragment 1

package com.example.fragments;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Frag1 extends Fragment {
final String LOG_TAG = "myLogs";

  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
    Log.d(LOG_TAG, "Fragment1 onAttach");
  }

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(LOG_TAG, "Fragment1 onCreate");
  }

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
  return inflater.inflate(R.layout.frag1, null);
  }

  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Log.d(LOG_TAG, "Fragment1 onActivityCreated");
    Log.d(LOG_TAG, "Fragment1 onCreateView");

  }

  public void onStart() {
    super.onStart();
    Log.d(LOG_TAG, "Fragment1 onStart");
  }

  public void onResume() {
    super.onResume();
    Log.d(LOG_TAG, "Fragment1 onResume");
  }

  public void onPause() {
    super.onPause();
    Log.d(LOG_TAG, "Fragment1 onPause");
  }

  public void onStop() {
    super.onStop();
    Log.d(LOG_TAG, "Fragment1 onStop");
  }

  public void onDestroyView() {
    super.onDestroyView();
    Log.d(LOG_TAG, "Fragment1 onDestroyView");
  }

  public void onDestroy() {
    super.onDestroy();
    Log.d(LOG_TAG, "Fragment1 onDestroy");
  }

  public void onDetach() {
    super.onDetach();
    Log.d(LOG_TAG, "Fragment1 onDetach");
  }
}

Fragment 2

package com.example.fragments;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Frag2 extends Fragment {
final String LOG_TAG = "myLogs";
 @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
    Log.d(LOG_TAG, "Fragment1 onAttach");
  }

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(LOG_TAG, "Fragment1 onCreate");
  }

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    Log.d(LOG_TAG, "Fragment1 onCreateView");
    return inflater.inflate(R.layout.frag2, null);
  }

  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Log.d(LOG_TAG, "Fragment1 onActivityCreated");

  }

  public void onStart() {
    super.onStart();
    Log.d(LOG_TAG, "Fragment1 onStart");
  }

  public void onResume() {
    super.onResume();
    Log.d(LOG_TAG, "Fragment1 onResume");
  }

  public void onPause() {
    super.onPause();
    Log.d(LOG_TAG, "Fragment1 onPause");
  }

  public void onStop() {
    super.onStop();
    Log.d(LOG_TAG, "Fragment1 onStop");
  }

  public void onDestroyView() {
    super.onDestroyView();
    Log.d(LOG_TAG, "Fragment1 onDestroyView");
  }

  public void onDestroy() {
    super.onDestroy();
    Log.d(LOG_TAG, "Fragment1 onDestroy");
  }

  public void onDetach() {
    super.onDetach();
    Log.d(LOG_TAG, "Fragment1 onDetach");
  }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<fragment
    android:name="ru.startandroid.develop.p1041fragmentlifecycle.Fragment1"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_weight="1" >
</fragment>

<fragment
    android:name="ru.startandroid.develop.p1041fragmentlifecycle.Fragment2"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_weight="1" >
</fragment>

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="frag1_text" >
</TextView>

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="frag2_text" >
</TextView>

Upvotes: 0

Views: 454

Answers (1)

buczek
buczek

Reputation: 2050

When you use

android:name="ru.startandroid.develop.p1041fragmentlifecycle.Fragment1"

you need to provide the package name. From your frag1.java file the package name is package com.example.fragments; So for the xml file you would want to use

android:name="com.example.fragments.Frag1"

or just for short:

android:name=".Frag1"

See this for more details.

Upvotes: 1

Related Questions