0

I am using below code to use fragment. I am developing it first time. Below code give me this error while I run code in emultor 4.2 or phone with android 2.3.3.

Error
Caused by: java.lang.ClassNotFoundException: com.example.android.fragments.ArticleFragment in loader dalvik.system.PathClassLoader[/data/app/com.example.fragmentdemo-2.apk]

Code:

MainActivity.java

public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_articles);

    }
}

ArticleFragment.java

public class ArticleFragment extends Fragment {
    public ArticleFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.article_fragment, container, false);
    }
}

HeadlinesFragment.java

public class HeadlinesFragment extends Fragment {
    public HeadlinesFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.head_lines_fragment, container, false);
    }
}

news_articles.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/head_lines_fragment"
        android:name="com.example.fragmentdemo.HeadlinesFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/article_fragment"
        android:name="com.example.android.fragments.ArticleFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

</LinearLayout>

article_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".FragmentDemo" >

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

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

</LinearLayout>

headlines_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Today's HeadLines"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.fragmentdemo"
    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.example.fragmentdemo.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>
3
  • Did this package com.example.android.fragments exists? Commented Sep 3, 2013 at 5:20
  • no only "com.example.fragmentdemo" this package is exists. Commented Sep 3, 2013 at 5:22
  • 1
    @nil then that is the location of your Article Fragment... com.example.fragmentdemo.ArticleFragment Commented Sep 3, 2013 at 6:19

3 Answers 3

2

in your news_article.xml you have written

<fragment
    android:id="@+id/article_fragment"
    android:name="com.example.android.fragments.ArticleFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2" />

does com.example.android.fragments.ArticleFragment really exist? I am guessing by looking at another fragment it should be com.example.fragmentdemo.ArticleFragment

Sign up to request clarification or add additional context in comments.

Comments

0

The error suggest to me that in your news_articles.xml file you aren't pointing to the correct location of your ArticlesFragment class.

Have a look at your file structure and the package your ArticleFragment is in and make sure it matches the path you are specifying in your xml file.

EDIT

From a comment on another answer I can see that the package you have specified ArticleFragment belongs to doesn't exist.

Change the path to your class in you main xml to com.example.fragmentdemo.ArticleFragment

Comments

0

dont user Fragment tags.

use com.example.android.fragments.ArticleFragment
instead. I am not clear about your path. It should "mypackage.ArticleFragment"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.