我正在使用片段为平板电脑和手机开发一个 android 应用程序。我按照教程http://www.vogella.com/articles/Android/article.html#fragments_tutorial创建了我自己的基础适配器类。实现的基本思想是mainactivity必须产生一个gridview,当点击gridview上的图像时,它必须在手机和平板电脑上打开一个新的activity,我们必须同时查看这两个activity。
但不知何故,我无法运行该应用程序。我开始时它正在崩溃,留下运行时错误。我是 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);
}
}
每个人Adapter.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>
详细信息.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