我还在学习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帮助。 提前谢谢。
答案 0 :(得分: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" />