无法使用片段为平板电脑和手机运行Android应用程序

时间:2012-07-10 17:29:18

标签: android android-layout android-fragments

我正在使用片段为平板电脑和手机开发Android应用程序。我正在学习教程http://www.vogella.com/articles/Android/article.html#fragments_tutorial并创建了我自己的基础适配器类。实现的基本思想是主要活动必须产生网格视图,当点击gridview上的图像时,它必须在手机和平​​板电脑上打开新活动,我们必须同时查看这两个活动。

但不知怎的,我无法运行该应用程序。它在我启动时崩溃,留下运行时错误。几个星期以来,我是刚开始研究这个问题的android开发人员。请帮帮我。谢谢。

请在下面的应用程序中找到我的课程

GridFragment.class

public class GridFragment extends Fragment{
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.gridview,container,false);
GridView gridView = (GridView) view.findViewById(R.id.gridView);
gridView.setAdapter(new EveryoneAdapter(view.getContext()));
// uses the view to get the context instead   of getActivity().
    return view;
}
 public void onActivityCreated(Bundle savedInstanceState) {
     super.onActivityCreated(savedInstanceState);
 }
}

EveryoneAdapter.class

public class EveryoneAdapter extends BaseAdapter {
private Context mContext;

public EveryoneAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(final int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
        imageView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
              Log.d("onClick","position ["+position+"]");
            }

          });
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7
};
}

DetailFragment.class

public class DetailFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}

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

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.details, container, false);
    return view;
}

  }

DetailActivity.class

public class DetailActivity extends Activity {
   @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Need to check if Activity has been switched to landscape mode
    // If yes, finished and go back to the start Activity
    if (getResources().getConfiguration().orientation == 
            Configuration.ORIENTATION_LANDSCAPE) {
        finish();
        return;
    }

    setContentView(R.layout.details_activity_layout);

}
  }

我的MainActivity.class

public class MainActivity extends Activity {

/** Called when the activity is first created. */

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

布局文件如下

main.xml(横向模式)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<fragment
    android:id="@+id/gridFragment"
    android:layout_width="150dip"
    android:layout_height="match_parent"
    android:layout_marginTop="?android:attr/actionBarSize"
    class="com.fxpal.unity.android.GridFragment" ></fragment>

<fragment
    android:id="@+id/detailFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.fxpal.unity.android.DetailFragment" >
    <!-- Preview: layout=@layout/details -->
</fragment>

details.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<fragment
    android:id="@+id/gridFragment"
    android:layout_width="150dip"
    android:layout_height="match_parent"
    android:layout_marginTop="?android:attr/actionBarSize"
    class="com.fxpal.unity.android.GridFragment" ></fragment>

<fragment
    android:id="@+id/detailFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.fxpal.unity.android.DetailFragment" >
    <!-- Preview: layout=@layout/details -->
</fragment>

details_activity_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<fragment
    android:id="@+id/gridFragment"
    android:layout_width="150dip"
    android:layout_height="match_parent"
    android:layout_marginTop="?android:attr/actionBarSize"
    class="com.fxpal.unity.android.GridFragment" ></fragment>

<fragment
    android:id="@+id/detailFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.fxpal.unity.android.DetailFragment" >
    <!-- Preview: layout=@layout/details -->
</fragment>

</LinearLayout>

main.xml(肖像模式)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<fragment
    android:id="@+id/gridFragment"
    android:layout_width="150dip"
    android:layout_height="match_parent"
    android:layout_marginTop="?android:attr/actionBarSize"
    class="com.fxpal.unity.android.GridFragment" ></fragment>

<fragment
    android:id="@+id/detailFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.fxpal.unity.android.DetailFragment" >
    <!-- Preview: layout=@layout/details -->
</fragment>

</LinearLayout>

我的Manifest.xml是

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fxpal.unity.android"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="List of Mobiles"
        android:name=".MainActivity" >
         <intent-filter >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".DetailActivity"
        android:label="@string/app_name" >
    </activity>
</application>

我的错误日志在

之下
07-10 04:06:06.625: E/AndroidRuntime(1393): FATAL EXCEPTION: main
07-10 04:06:06.625: E/AndroidRuntime(1393): java.lang.RuntimeException: Unable to start activity      ComponentInfo{com.fxpal.unity.android/com.fxpal.unity.android.MainActivity}: android.view.InflateException:   Binary XML file line #7: Error inflating class fragment
07-10 04:06:06.625: E/AndroidRuntime(1393):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at android.os.Looper.loop(Looper.java:137)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at android.app.ActivityThread.main(ActivityThread.java:4424)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at java.lang.reflect.Method.invokeNative(Native Method)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at java.lang.reflect.Method.invoke(Method.java:511)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at dalvik.system.NativeStart.main(Native Method)
07-10 04:06:06.625: E/AndroidRuntime(1393): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
07-10 04:06:06.625: E/AndroidRuntime(1393):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at android.app.Activity.setContentView(Activity.java:1835)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at com.fxpal.unity.android.MainActivity.onCreate(MainActivity.java:14)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at android.app.Activity.performCreate(Activity.java:4465)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-10 04:06:06.625: E/AndroidRuntime(1393):     ... 11 more
07-10 04:06:06.625: E/AndroidRuntime(1393): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.fxpal.unity.android.ListFragment: make sure class name exists, is public, and has an empty constructor that is public
07-10 04:06:06.625: E/AndroidRuntime(1393):     at android.app.Fragment.instantiate(Fragment.java:581)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at android.app.Fragment.instantiate(Fragment.java:549)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at android.app.Activity.onCreateView(Activity.java:4235)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
07-10 04:06:06.625: E/AndroidRuntime(1393):     ... 21 more
07-10 04:06:06.625: E/AndroidRuntime(1393): Caused by: java.lang.ClassNotFoundException: com.fxpal.unity.android.ListFragment
07-10 04:06:06.625: E/AndroidRuntime(1393):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
07-10 04:06:06.625: E/AndroidRuntime(1393):     at android.app.Fragment.instantiate(Fragment.java:571)
07-10 04:06:06.625: E/AndroidRuntime(1393):     ... 24 more

2 个答案:

答案 0 :(得分:1)

要么您没有复制粘贴您正在使用的所有布局文件,要么您在某处加载了错误的布局文件。 stacktrace清楚地提到,在某些时候它会尝试从布局文件中加载具有完全限定类名com.fxpal.unity.android.ListFragment的片段。但是,您提供的代码段中没有ListFragment类。

在您的项目中搜索com.fxpal.unity.android.ListFragment并更正任何引用。

答案 1 :(得分:0)

尝试在布局xml中使用anodrid:name=""而不是class=