2014-06-06 150 views
0

我仍然在學習Android編程,有這個應用程序,它給了我不幸的停止錯誤。Android應用程序,不幸的應用程序已停止

MainActivity

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(片段)

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

plz幫助。 在此先感謝。

+1

發佈您的logcat錯誤。 –

+0

[LogCat](http://developer.android.com/tools/help/logcat.html)? – Skynet

+0

你在哪裏實現接口? – Raghunandan

回答

1

您需要擴展Activity爲:

public class MainActivity extends FragmentActivity implements OnFragmentInteractionListener{ 

    .... 
} 

,並在此Activity您需要定義方法:

@Override 
public void onFragmentInteraction(Uri uri)(

    // do whatever you wish with the uri 
} 

此外,從你的清單中刪除以下行:

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

非常感謝它的工作。 –

+0

不客氣...... :) –

相關問題