フラグメントを使用して、タブレットと電話の両方で Android アプリケーションを開発しています。私はチュートリアルhttp://www.vogella.com/articles/Android/article.html#fragments_tutorialに従っていて、独自の基本アダプター クラスを作成しました。実装の基本的な考え方は、メインアクティビティがグリッドビューを生成する必要があり、グリッドビューの画像をクリックすると、電話で新しいアクティビティを開く必要があり、タブレットで 2 つのアクティビティを同時に表示する必要があるということです。
しかし、どういうわけか、アプリケーションを実行できません。起動中にクラッシュし、ランタイム エラーが発生します。私は数週間からこの問題に取り組んでいるアンドロイド開発に不慣れです。助けてください。よろしくお願いします。
アプリケーションで私のクラスの下を見つけてください
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
};
}
詳細フラグメント.クラス
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;
}
}
詳細アクティビティ.クラス
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