0

以下のコードを使用してフラグメントを使用しています。初めて開発します。以下のコードは、エミュレータ 4.2 または Android 2.3.3 を搭載した電話でコードを実行しているときにこのエラーを表示します。

エラー
の原因: java.lang.ClassNotFoundException: ローダー dalvik.system.PathClassLoader の com.example.android.fragments.ArticleFragment[/data/app/com.example.fragmentdemo-2.apk]

コード:

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>
4

3 に答える 3

2

あなたnews_article.xmlが書いた

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

com.example.android.fragments.ArticleFragment本当に存在しますか?別のフラグメントを見て推測していますcom.example.fragmentdemo.ArticleFragment

于 2013-09-03T05:52:11.823 に答える
0

Fragment タグを使用しないでください。


代わりにcom.example.android.fragments.ArticleFragment を使用してください。私はあなたの道について明確ではありません。「mypackage.ArticleFragment」にする必要があります

于 2013-09-03T06:42:50.887 に答える
0

このエラーは、news_articles.xml ファイルで、ArticlesFragment クラスの正しい場所を指していないことを示唆しています。

ファイル構造と ArticleFragment が入っているパッケージを調べて、xml ファイルで指定しているパスと一致していることを確認してください。

編集

ArticleFragment別の回答のコメントから、指定したパッケージが存在しないことがわかります。

メイン xml のクラスへのパスを次のように変更します。com.example.fragmentdemo.ArticleFragment

于 2013-09-03T05:20:55.080 に答える