我正在尝试使用Android Application Developement for Dummies这本书来学习一些Android编程。本书的一个任务是创建一个名为任务提醒的应用程序,它引入了android片段。根据该书,下面的编码应该创建第一个简单的列表视图:
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
public class ReminderListActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reminder_list);
}
}
提醒名单如下:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.holtkoe.android.birthdaytimer.ReminderListFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
ReminderListFragment类如下:
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
public class ReminderListFragment extends ListFragment {
private ListAdapter mAdapter;
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
String[] items = new String[] {"Foo","Bar","Fizz","Bin"};
mAdapter = new ArrayAdapter<String>(getActivity(), R.layout.reminder_row, R.id.text1, items);
setListAdapter(mAdapter);
}
}
最后提醒_row:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/text1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"/>
我尝试在模拟设备和智能手机上运行此功能但总是得到以下内容
09-26 22:44:53.270: D/AndroidRuntime(2493): Shutting down VM
09-26 22:44:53.270: W/dalvikvm(2493): threadid=1: thread exiting with uncaught exception (group=0x412d8930)
09-26 22:44:53.290: E/AndroidRuntime(2493): FATAL EXCEPTION: main
09-26 22:44:53.290: E/AndroidRuntime(2493): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.holtkoe.android.birthdaytimer/com.holtkoe.android.birthdaytimer.ReminderListActivity}: android.view.InflateException: Binary XML file line #3: Error inflating class fragment
09-26 22:44:53.290: E/AndroidRuntime(2493): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
09-26 22:44:53.290: E/AndroidRuntime(2493): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
09-26 22:44:53.290: E/AndroidRuntime(2493): at android.app.ActivityThread.access$700(ActivityThread.java:150)
09-26 22:44:53.290: E/AndroidRuntime(2493): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
09-26 22:44:53.290: E/AndroidRuntime(2493): at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 22:44:53.290: E/AndroidRuntime(2493): at android.os.Looper.loop(Looper.java:137)
09-26 22:44:53.290: E/AndroidRuntime(2493): at android.app.ActivityThread.main(ActivityThread.java:5283)
09-26 22:44:53.290: E/AndroidRuntime(2493): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 22:44:53.290: E/AndroidRuntime(2493): at java.lang.reflect.Method.invoke(Method.java:511)
09-26 22:44:53.290: E/AndroidRuntime(2493): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
09-26 22:44:53.290: E/AndroidRuntime(2493): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
09-26 22:44:53.290: E/AndroidRuntime(2493): at dalvik.system.NativeStart.main(Native Method)
09-26 22:44:53.290: E/AndroidRuntime(2493): Caused by: android.view.InflateException: Binary XML file line #3: Error inflating class fragment
09-26 22:44:53.290: E/AndroidRuntime(2493): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
09-26 22:44:53.290: E/AndroidRuntime(2493): at android.view.LayoutInflater.inflate(LayoutInflater.java:467)
09-26 22:44:53.290: E/AndroidRuntime(2493): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
09-26 22:44:53.290: E/AndroidRuntime(2493): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
09-26 22:44:53.290: E/AndroidRuntime(2493): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:364)
09-26 22:44:53.290: E/AndroidRuntime(2493): at android.app.Activity.setContentView(Activity.java:1930)
09-26 22:44:53.290: E/AndroidRuntime(2493): at com.holtkoe.android.birthdaytimer.ReminderListActivity.onCreate(ReminderListActivity.java:14)
09-26 22:44:53.290: E/AndroidRuntime(2493): at android.app.Activity.performCreate(Activity.java:5283)
09-26 22:44:53.290: E/AndroidRuntime(2493): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
09-26 22:44:53.290: E/AndroidRuntime(2493): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
09-26 22:44:53.290: E/AndroidRuntime(2493): ... 11 more
09-26 22:44:53.290: E/AndroidRuntime(2493): Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.holtkoe.android.birthdaytimer.ReminderListFragment that is not a Fragment
09-26 22:44:53.290: E/AndroidRuntime(2493): at android.app.Fragment.instantiate(Fragment.java:584)
09-26 22:44:53.290: E/AndroidRuntime(2493): at android.app.Fragment.instantiate(Fragment.java:560)
09-26 22:44:53.290: E/AndroidRuntime(2493): at android.app.Activity.onCreateView(Activity.java:4864)
09-26 22:44:53.290: E/AndroidRuntime(2493): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
09-26 22:44:53.290: E/AndroidRuntime(2493): ... 20 more
09-26 22:44:53.290: E/AndroidRuntime(2493): Caused by: java.lang.ClassCastException
09-26 22:44:53.290: E/AndroidRuntime(2493): ... 24 more
说实话,我迷路了(显然甚至不是假人)......我阅读了Android开发者参考资料中的片段,其中给出的示例与本书中的内容非常相似,但由于某种原因,是一个通货膨胀问题。
我非常感谢任何帮助!谢谢,伙计们!
答案 0 :(得分:2)
我认为您缺少一个onCreateView方法来夸大您的视图。您仍然需要在片段内膨胀视图。
即
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
public class ReminderListFragment extends ListFragment {
private ListAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.reminder_list, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
String[] items = new String[] {"Foo","Bar","Fizz","Bin"};
mAdapter = new ArrayAdapter<String>(getActivity(), R.layout.reminder_row,
R.id.text1, items);
setListAdapter(mAdapter);
}
}
编辑:
您可以尝试另外一件事,在您给我的示例中,在xml中片段位于框架布局中,您可以尝试将xml更改为如下所示:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.holtkoe.android.birthdaytimer.ReminderListFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
尝试使用和不使用onCreateView来查看所有选项以及它们的行为方式。
答案 1 :(得分:0)
您不能混合平台片段(android.app.Fragment
等)并支持库片段(android.support.v4.app.Fragment
)。
如果您打算使用<fragment>
代码对支持库片段进行充气,那么您的活动应该扩展支持库FragmentActivity
,而不是ListActivity
。
答案 2 :(得分:0)
最初我根据我在开发人员资源中使用fragmentlist读到的内容将片段标记更改为listview标记...至少我没有遇到任何豁免,但列表项尚未显示。我尝试了对textview布局的一些更改,但最终放弃了这一点。
然后我在网上发现了一个简单的listfragment示例,该示例仅执行了API级别14,因此我只是将其用作最低级别并相应地调整了代码(从头开始,因为我搞砸了之前的代码)。显然,不需要支持库,它可以正常工作。因此,我将继续使用此API级别,并且一旦我更有经验,将尝试降低运行级别; - )
这是现在可以使用的代码:
package de.holtkoe.birthdayreminder;
import android.app.Activity;
import android.os.Bundle;
public class ReminderListActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reminder_list);
}
}
布局资源reminder_list:
<LinearLayout 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">
<fragment
android:id="@+id/fragment"
android:name="de.holtkoe.birthdayreminder.ReminderListFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
片段活动再次是ReminderListFragment:
package de.holtkoe.birthdayreminder;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
public class ReminderListFragment extends ListFragment {
private ListAdapter mAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
super.onCreate(savedInstanceState);
String[] items = new String[] {"Foo","Bar","Fizz","Bin"};
mAdapter = new ArrayAdapter<String>(inflater.getContext(), R.layout.reminder_row, items);
setListAdapter(mAdapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
}
最后提醒_row保持不变。