Reputation: 171
I am trying to implement the same example as on developers.android page but there is a problem with my implementation which crashes the activity. Ok I have two activity layouts, one for portrait(uses two activities to get the work done) and one for landscape (which uses only one activity as both the list and details are shown in the same activity. My application works like a charm in landscape mode but crashes like hell in portrait mode so I am pasting the code for the portrait mode only please have a look:
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);
}
}
please note that this activity is started by the main activity if the device is in portrait mode. In the intent I am getting data(ListView position(int)) from the main activity which was given to the main activity by the First fragment(contains the listview). Code for fragment2 is here so you can understand the "onReply" method:
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]);
}
}
The problem is that whenever in portrait mode I press a list item, app crashes. Also note that the application's crashing happen on "
if(frag != null )
frag.onReply(data);
" in the above "OtherActivity" class. JIC if you want the catlog information, here it is:
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
The above catLog information is just about errors.
Upvotes: 1
Views: 395
Reputation: 14590
getView()
returns the View
after onCrateView()
called otherwise it returns null
Change this line in onCreateView
tv = (TextView) getView().findViewById(R.id.textView1);
into
tv = (TextView) view.findViewById(R.id.textView1);
that too change this line
public class OtherActivity extends Activity
into
public class OtherActivity extends FragmentActivity
And import Fragment
and FragmentActivity
from the support library
Use getSupportFragmentManager()
instead getFragmentManager()
Upvotes: 2
Reputation: 49817
The problem is this line:
tv = (TextView) getView().findViewById(R.id.textView1);
In onCreateView()
the View
has not yet been set, therefor getView()
returns null. Try this:
tv = (TextView) view.findViewById(R.id.textView1);
Upvotes: 1
Reputation: 47817
You should change this
tv = (TextView) getView().findViewById(R.id.textView1);
to
tv = (TextView) view.findViewById(R.id.textView1);
in your onCrateView(....)
in your Fragment2
Upvotes: 2