0

在我的项目中,我有两个类 1).FragmentTestActivty--------扩展 FragmentActivty 2).TestFragment ------------- -----扩展片段

FragmentTestActivty.java

          import android.support.v4.app.Fragment;
            import android.support.v4.app.FragmentActivity;
           import android.support.v4.app.FragmentTransaction;



          public class FragmentTestActivity extends FragmentActivity implements
    OnItemClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {// first
    System.out.println("1");
    super.onCreate(savedInstanceState);
    setContentView(com.example.fragmenttest.R.layout.main);

            l = (ListView) findViewById(com.example.fragmenttest.R.id.number_list);

    l.setAdapter(new MyCustomAdapter());


    l.setOnItemClickListener(FragmentTestActivity.this);


        /** Called when a number gets clicked */
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    number = position;
    System.out.println("clicking or not");
    name = option[number];
    Fragment f = new TestFragment(position + 1);
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.the_frag, f);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.addToBackStack(null);
    ft.commit();

}

}

测试片段.java

               import android.support.v4.app.Fragment;
              import android.view.LayoutInflater;
              import android.view.View;


                  public class TestFragment extends Fragment {


public TestFragment() {

}

/* Constructor for being created explicitly */
public TestFragment(int nAndroids) {
    this.nAndroids = nAndroids;


}

/* If we are being created with saved state, restore our state */

public void onCreate(Bundle saved) {
    super.onCreate(saved);

    if (null != saved) {
        nAndroids = saved.getInt("nAndroids");

    }
}



public void onSaveInstanceState(Bundle toSave) {
    toSave.putInt("nAndroids", nAndroids);

}



View v;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle saved) {

    c = getActivity().getApplicationContext();

    LayoutInflater in = getActivity().getLayoutInflater();
    v = in.inflate(R.layout.list, null);

    LinearLayout lf = (LinearLayout) v.findViewById(R.id.frgmentpart);

    System.out.println(nAndroids + "n android");
    ListView lv2 = (ListView) v.findViewById(R.id.listView2);

    if (nAndroids == 1) {
        lv2 = (ListView) v.findViewById(R.id.listView2);
        lv2.setAdapter(new listview2());
        ;
        System.out.println("inside");

    } else if (nAndroids > 1) {
        System.out.println("outside " + lf);
        lf.setVisibility(View.GONE);
        // v=in.inflate(R.layout.list2, null);
    }
    return v;

}

}

错误

06-05 16:25:41.535: E/AndroidRuntime(335): FATAL EXCEPTION: main

06-05 16:25:41.535: E/AndroidRuntime(335): java.lang.RuntimeException: Unable to instantiate activity 
ComponentInfo{com.example.fragmenttest/com.example.fragmenttest.FragmentTestActivity}: java.lang.ClassNotFoundException: com.example.fragmenttest.FragmentTestActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.fragmenttest-2.apk]

             06-05 16:25:41.535: E/AndroidRuntime(335):     at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
            06-05 16:25:41.535: E/AndroidRuntime(335):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
              06-05 16:25:41.535: E/AndroidRuntime(335):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-05 16:25:41.535: E/AndroidRuntime(335):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-05 16:25:41.535: E/AndroidRuntime(335):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-05 16:25:41.535: E/AndroidRuntime(335):  at android.os.Looper.loop(Looper.java:123)
06-05 16:25:41.535: E/AndroidRuntime(335):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-05 16:25:41.535: E/AndroidRuntime(335):  at java.lang.reflect.Method.invokeNative(Native Method)
06-05 16:25:41.535: E/AndroidRuntime(335):  at java.lang.reflect.Method.invoke(Method.java:507)
06-05 16:25:41.535: E/AndroidRuntime(335):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-05 16:25:41.535: E/AndroidRuntime(335):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-05 16:25:41.535: E/AndroidRuntime(335):  at dalvik.system.NativeStart.main(Native Method)
06-05 16:25:41.535: E/AndroidRuntime(335): Caused by: java.lang.ClassNotFoundException: com.example.fragmenttest.FragmentTestActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.fragmenttest-2.apk]
06-05 16:25:41.535: E/AndroidRuntime(335):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
06-05 16:25:41.535: E/AndroidRuntime(335):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
06-05 16:25:41.535: E/AndroidRuntime(335):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
06-05 16:25:41.535: E/AndroidRuntime(335):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
06-05 16:25:41.535: E/AndroidRuntime(335):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
06-05 16:25:41.535: E/AndroidRuntime(335):  ... 11 more

清单文件

              ?xml version="1.0" encoding="utf-8"?>
       <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.fragmenttest"
   >
<uses-sdk android:minSdkVersion="7"
    android:targetSdkVersion="10" />

<application 
android:icon="@drawable/icon" 
android:label="@string/app_name"
android:theme="@android:style/Theme.Black"


>
    <activity android:name=".FragmentTestActivity"
              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>

图片 |--------------------------| | | | | | | | | 片段 | |活动| | | |testfragment.java| | | | | | | |----- ---------| |

4

1 回答 1

0

您需要做的就是在 Eclipse 中右键单击我的项目并选择 Android Tools-->Add Support Library...

于 2012-06-05T11:14:51.037 回答