4

水平スワイプを実装する方法を学んでいますが、レイアウトにあるアプリを起動しようとすると、次のエラーが発生しViewPagerます。

03-25 17:12:13.166: E/AndroidRuntime(19449): FATAL EXCEPTION: main
03-25 17:12:13.166: E/AndroidRuntime(19449): java.lang.RuntimeException: Unable to start activity ComponentInfo{androidapps.viewpagerdemo/androidapps.viewpagerdemo.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class android.support.v4.app.ViewPager
03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.os.Looper.loop(Looper.java:137)
03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.app.ActivityThread.main(ActivityThread.java:4745)
03-25 17:12:13.166: E/AndroidRuntime(19449):    at java.lang.reflect.Method.invokeNative(Native Method)
03-25 17:12:13.166: E/AndroidRuntime(19449):    at java.lang.reflect.Method.invoke(Method.java:511)
03-25 17:12:13.166: E/AndroidRuntime(19449):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-25 17:12:13.166: E/AndroidRuntime(19449):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-25 17:12:13.166: E/AndroidRuntime(19449):    at dalvik.system.NativeStart.main(Native Method)
03-25 17:12:13.166: E/AndroidRuntime(19449): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class android.support.v4.app.ViewPager
03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-25 17:12:13.166: E/AndroidRuntime(19449):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.app.Activity.setContentView(Activity.java:1867)
03-25 17:12:13.166: E/AndroidRuntime(19449):    at androidapps.viewpagerdemo.MainActivity.onCreate(MainActivity.java:14)
03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.app.Activity.performCreate(Activity.java:5008)
03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-25 17:12:13.166: E/AndroidRuntime(19449):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-25 17:12:13.166: E/AndroidRuntime(19449):    ... 11 more

このリンクの解決策を試しましたが、うまくいきませんでした。 クラス android.support.v4.view.ViewPager の膨張エラー

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"
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=".MainActivity" >

<android.support.v4.app.ViewPager 
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

</RelativeLayout>

フラグメントのレイアウト ファイルは、1 つのTextView. フラグメント クラスは次のとおりです。

public class MyFragment extends Fragment{
int mCurrentPage;

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

    /** Getting the arguments to the Bundle object */
    Bundle data = getArguments();

    /** Getting integer data of the key current_page from the bundle */
    mCurrentPage = data.getInt("current_page", 0);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.frag_layout, container,false);
    TextView tv = (TextView ) v.findViewById(R.id.tv);
    tv.setText("You are viewing the page #" + mCurrentPage + "\n\n" + "Swipe Horizontally left / right");
    return v;
}
}

FragmentPagerAdapter クラス:

public class MyAdapter extends FragmentPagerAdapter{

final int PAGE_COUNT = 3;
public MyAdapter(FragmentManager fm) {
    super(fm);
    // TODO Auto-generated constructor stub
}

@Override
public Fragment getItem(int arg0) {
    // TODO Auto-generated method stub
    MyFragment myFragment = new MyFragment();
    Bundle data = new Bundle();
    data.putInt("current_page", arg0+1);
    myFragment.setArguments(data);
    return myFragment;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return PAGE_COUNT;
}
}

主な活動:

public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ViewPager pager = (ViewPager) findViewById(R.id.pager);

    /** Instantiating FragmentPagerAdapter */
    MyAdapter pagerAdapter = new MyAdapter(getSupportFragmentManager());

    /** Setting the pagerAdapter to the pager object */
    pager.setAdapter(pagerAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}
4

1 に答える 1

4

こんにちは、レイアウト ファイルに変更を加えました。現在は正常に動作しています。

<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:padding="10dp" >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

これを確認できます:

ここに画像の説明を入力

以下のコードは参考用です---->

あなたの主な活動:

package com.example.viewpage;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class MainActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewPager pager = (ViewPager) findViewById(R.id.pager);

        /** Instantiating FragmentPagerAdapter */
        MyAdapter pagerAdapter = new MyAdapter(getSupportFragmentManager());

        /** Setting the pagerAdapter to the pager object */
        pager.setAdapter(pagerAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

マイアダプター:

package com.example.viewpage;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

public class MyAdapter extends android.support.v4.app.FragmentPagerAdapter {

    final int PAGE_COUNT = 3;

    public MyAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub
        MyFragment myFragment = new MyFragment();
        Bundle data = new Bundle();
        data.putInt("current_page", arg0 + 1);
        myFragment.setArguments(data);
        return myFragment;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return PAGE_COUNT;
    }

}

マイフラグメント:

package com.example.viewpage;

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 MyFragment extends Fragment {

    private int mCurrentPage;

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

        /** Getting the arguments to the Bundle object */
        Bundle data = getArguments();

        /** Getting integer data of the key current_page from the bundle */
        mCurrentPage = data.getInt("current_page", 0);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.frag_layout, container, false);
        TextView tv = (TextView) v.findViewById(R.id.textView1);
        tv.setText("You are viewing the page #" + mCurrentPage + "\n\n"
                + "Swipe Horizontally left / right");
        return v;
    }
}

frag_layout.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

注:android-support-v4.jar lib フォルダーにサポートを追加します。この jar を に追加しBuild Pathます。に行きproject property-->Build Path--> Order & Export--> select all --> okます。クリーン&ラン。

于 2013-03-25T12:22:49.873 に答える