我正在尝试实现与developers.android页面相同的示例,但是我的实现存在问题,导致活动崩溃。 好的我有两个活动布局,一个用于肖像(使用两个活动完成工作)和一个用于横向(仅使用一个活动,因为列表和细节都显示在同一个活动中。我的应用程序就像一个魅力横向模式,但在肖像模式下像地狱一样崩溃所以我只是粘贴肖像模式的代码请看看:
public class OtherActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity);
Intent intent = getIntent();
int data = intent.getIntExtra("key", 2);
Fragment2 frag = (Fragment2) getFragmentManager().findFragmentById(R.id.fragment2);
if(frag != null )
frag.onReply(data);
}
}
请注意,如果设备处于纵向模式,此活动将由主活动启动。在意图中,我从主要活动获取数据(ListView position(int)),该活动由First片段(包含listview)提供给主活动。 fragment2的代码在这里,所以你可以理解“onReply”方法:
public class Fragment2 extends Fragment {
TextView tv;
String[] items = {
"An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface. The window typically fills the screen, but may be smaller than the screen and float on top of other windows.",
"A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.",
"Content providers manage access to a structured set of data. They encapsulate the data, and provide mechanisms for defining data security. Content providers are the standard interface that connects data in one process with code running in another process.",
"An Intent is a messaging object you can use to request an action from another app component. Although intents facilitate communication between components in several ways, there are three fundamental use-cases." };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.frag2, container, false);
tv = (TextView) getView().findViewById(R.id.textView1);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
public void onReply(int data){
tv.setText(items[data]);
}
}
问题在于,无论何时在纵向模式下,我按下列表项,应用程序崩溃。另请注意,应用程序的崩溃发生在“
上if(frag != null )
frag.onReply(data);
“在上面的”OtherActivity“类中。 JIC如果你想要catlog信息,请点击这里:
06-20 06:33:47.564: E/cutils-trace(2946): Error opening trace file: No such file or directory (2)
06-20 06:33:49.134: E/cutils-trace(2957): Error opening trace file: No such file or directory (2)
06-20 06:33:55.814: E/SoundPool(287): error loading /system/media/audio/ui/Effect_Tick.ogg
06-20 06:33:55.824: E/SoundPool(287): error loading /system/media/audio/ui/Effect_Tick.ogg
06-20 06:33:55.834: E/SoundPool(287): error loading /system/media/audio/ui/Effect_Tick.ogg
06-20 06:33:55.844: E/SoundPool(287): error loading /system/media/audio/ui/Effect_Tick.ogg
06-20 06:33:55.854: E/SoundPool(287): error loading /system/media/audio/ui/Effect_Tick.ogg
06-20 06:33:55.874: E/SoundPool(287): error loading /system/media/audio/ui/KeypressStandard.ogg
06-20 06:33:55.884: E/SoundPool(287): error loading /system/media/audio/ui/KeypressSpacebar.ogg
06-20 06:33:55.884: E/SoundPool(287): error loading /system/media/audio/ui/KeypressDelete.ogg
06-20 06:33:55.904: E/SoundPool(287): error loading /system/media/audio/ui/KeypressReturn.ogg
06-20 06:33:56.644: E/AndroidRuntime(2968): FATAL EXCEPTION: main
06-20 06:33:56.644: E/AndroidRuntime(2968): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentflexibleui2/com.example.fragmentflexibleui2.OtherActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
06-20 06:33:56.644: E/AndroidRuntime(2968): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
06-20 06:33:56.644: E/AndroidRuntime(2968): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
06-20 06:33:56.644: E/AndroidRuntime(2968): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-20 06:33:56.644: E/AndroidRuntime(2968): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
06-20 06:33:56.644: E/AndroidRuntime(2968): at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 06:33:56.644: E/AndroidRuntime(2968): at android.os.Looper.loop(Looper.java:137)
06-20 06:33:56.644: E/AndroidRuntime(2968): at android.app.ActivityThread.main(ActivityThread.java:5103)
06-20 06:33:56.644: E/AndroidRuntime(2968): at java.lang.reflect.Method.invokeNative(Native Method)
06-20 06:33:56.644: E/AndroidRuntime(2968): at java.lang.reflect.Method.invoke(Method.java:525)
06-20 06:33:56.644: E/AndroidRuntime(2968): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-20 06:33:56.644: E/AndroidRuntime(2968): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-20 06:33:56.644: E/AndroidRuntime(2968): at dalvik.system.NativeStart.main(Native Method)
06-20 06:33:56.644: E/AndroidRuntime(2968): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
06-20 06:33:56.644: E/AndroidRuntime(2968): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
06-20 06:33:56.644: E/AndroidRuntime(2968): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
06-20 06:33:56.644: E/AndroidRuntime(2968): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
06-20 06:33:56.644: E/AndroidRuntime(2968): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-20 06:33:56.644: E/AndroidRuntime(2968): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
06-20 06:33:56.644: E/AndroidRuntime(2968): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
06-20 06:33:56.644: E/AndroidRuntime(2968): at android.app.Activity.setContentView(Activity.java:1895)
06-20 06:33:56.644: E/AndroidRuntime(2968): at com.example.fragmentflexibleui2.OtherActivity.onCreate(OtherActivity.java:13)
06-20 06:33:56.644: E/AndroidRuntime(2968): at android.app.Activity.performCreate(Activity.java:5133)
06-20 06:33:56.644: E/AndroidRuntime(2968): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-20 06:33:56.644: E/AndroidRuntime(2968): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
06-20 06:33:56.644: E/AndroidRuntime(2968): ... 11 more
06-20 06:33:56.644: E/AndroidRuntime(2968): Caused by: java.lang.NullPointerException
06-20 06:33:56.644: E/AndroidRuntime(2968): at com.example.fragmentflexibleui2.Fragment2.onCreateView(Fragment2.java:24)
06-20 06:33:56.644: E/AndroidRuntime(2968): at android.app.Fragment.performCreateView(Fragment.java:1695)
06-20 06:33:56.644: E/AndroidRuntime(2968): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:861)
06-20 06:33:56.644: E/AndroidRuntime(2968): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
06-20 06:33:56.644: E/AndroidRuntime(2968): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137)
06-20 06:33:56.644: E/AndroidRuntime(2968): at android.app.Activity.onCreateView(Activity.java:4746)
06-20 06:33:56.644: E/AndroidRuntime(2968): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
06-20 06:33:56.644: E/AndroidRuntime(2968): ... 21 more
06-20 06:33:58.873: E/SoundPool(287): error loading /system/media/audio/ui/Effect_Tick.ogg
06-20 06:33:58.883: E/SoundPool(287): error loading /system/media/audio/ui/Effect_Tick.ogg
06-20 06:33:58.883: E/SoundPool(287): error loading /system/media/audio/ui/Effect_Tick.ogg
06-20 06:33:58.893: E/SoundPool(287): error loading /system/media/audio/ui/Effect_Tick.ogg
06-20 06:33:58.903: E/SoundPool(287): error loading /system/media/audio/ui/Effect_Tick.ogg
06-20 06:33:58.913: E/SoundPool(287): error loading /system/media/audio/ui/KeypressStandard.ogg
06-20 06:33:58.933: E/SoundPool(287): error loading /system/media/audio/ui/KeypressSpacebar.ogg
06-20 06:33:58.983: E/SoundPool(287): error loading /system/media/audio/ui/KeypressDelete.ogg
06-20 06:33:58.993: E/SoundPool(287): error loading /system/media/audio/ui/KeypressReturn.ogg
以上catLog信息只是关于错误。
答案 0 :(得分:3)
getView()
在View
调用后返回onCrateView()
,否则返回null
在onCreateView中更改此行
tv = (TextView) getView().findViewById(R.id.textView1);
进入
tv = (TextView) view.findViewById(R.id.textView1);
这也改变了这一行
public class OtherActivity extends Activity
到
public class OtherActivity extends FragmentActivity
从支持库中导入Fragment
和FragmentActivity
使用getSupportFragmentManager()
代替getFragmentManager()
答案 1 :(得分:2)
你应该改变这个
tv = (TextView) getView().findViewById(R.id.textView1);
到
tv = (TextView) view.findViewById(R.id.textView1);
在onCrateView(....)
Fragment2
答案 2 :(得分:1)
问题在于这一行:
tv = (TextView) getView().findViewById(R.id.textView1);
在onCreateView()
中尚未设置View
,因此getView()
返回null。试试这个:
tv = (TextView) view.findViewById(R.id.textView1);