java - Android 应用程序不幸的是应用程序已停止

标签 java android xml

我仍在学习 Android 编程,有这个应用程序,它给了我不幸的停止错误。

主要 Activity

package com.nyt.ilm.ilmsarf;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity {

    ViewPager viewpager;
    @Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);
        setContentView(R.layout.activity_main);
        viewpager = (ViewPager) findViewById(R.id.pager);
        viewpager.setAdapter(new MyPageAdapter(getSupportFragmentManager()));
    }

}

class MyPageAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub
        Fragment fr=null;
        if (arg0==0)
                fr=new P1();
        if(arg0==1)
            fr=new P2();
        if(arg0==2)
            fr=new P3();
        return fr;
    }

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

}

P1.java( fragment )

 package com.nyt.ilm.ilmsarf;

    import android.app.Activity;
    import android.net.Uri;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    public class P1 extends Fragment {

        private OnFragmentInteractionListener mListener;

        public P1() {
            // Required empty public constructor
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.p1, container, false);
        }

        // TODO: Rename method, update argument and hook method into UI event
        public void onButtonPressed(Uri uri) {
            if (mListener != null) {
                mListener.onFragmentInteraction(uri);
            }
        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            try {
                mListener = (OnFragmentInteractionListener) activity;
            } catch (ClassCastException e) {
                throw new ClassCastException(activity.toString()
                        + " must implement OnFragmentInteractionListener");
            }
        }

        @Override
        public void onDetach() {
            super.onDetach();
            mListener = null;
        }

        /**
         * This interface must be implemented by activities that contain this
         * fragment to allow an interaction in this fragment to be communicated to
         * the activity and potentially other fragments contained in that activity.
         * <p>
         * See the Android Training lesson <a href=
         * "http://developer.android.com/training/basics/fragments/communicating.html"
         * >Communicating with Other Fragments</a> for more information.
         */
        public interface OnFragmentInteractionListener {
            // TODO: Update argument type and name
            public void onFragmentInteraction(Uri uri);
        }

    }

P1.xml

<FrameLayout 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="com.nyt.ilm.ilmsarf.P1" >

    <!-- TODO: Update blank fragment layout -->

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/sarf01" />

</FrameLayout>

activity.xml 是

 <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" />

AndroidManifest.xml 是

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nyt.ilm.ilmsarf"
    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.nyt.ilm.ilmsarf.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>

         <activity android:name="com.nyt.ilm.ilmsarf.P1" />
             <activity android:name="com.nyt.ilm.ilmsarf.P2" />
              <activity android:name="com.nyt.ilm.ilmsarf.P3" />
    </application>

</manifest>

P2、P3为p1.xml和P1.java

请帮忙。 提前致谢。

最佳答案

您需要将您的Activity扩展为:

public class MainActivity extends FragmentActivity implements OnFragmentInteractionListener{

    ....
}

在此Activity中,您需要定义方法:

@Override
public void onFragmentInteraction(Uri uri)(

    // do whatever you wish with the uri
}

此外,请从 list 中删除以下行:

<activity android:name="com.nyt.ilm.ilmsarf.P1" />
<activity android:name="com.nyt.ilm.ilmsarf.P2" />
<activity android:name="com.nyt.ilm.ilmsarf.P3" />

关于java - Android 应用程序不幸的是应用程序已停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24076508/

相关文章:

php - Android - 上传到 PHP 服务器时图像变空

android - 通过蓝牙发送多个文件

android - OAuth 1.0 与 Android 4.x 和抄写员

javascript - 无法使用PHP将数据从javascript保存到MySql

xml - 使用自定义 UI 编辑器插入新选项卡作为 Excel 功能区中的最后一个选项卡

java - JMenuItem 显示奇怪的名称

java - 在 Java EE 中组织诊断测试的正确方法

java - settings.xml 中的 Maven 命令行参数

java - 在 JDBC/PostgreSQL 中使用 SQL 数组类型更新结果集

java - 放心 jsonpath findAll 没有按预期工作