1

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.

Saad
  • 171
  • 6

3 Answers3

2

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

M D
  • 47,665
  • 9
  • 93
  • 114
2

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()

kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
  • why is the application crashing if I put tv = (TextView) getView().findViewById(R.id.textView1); in onActivityCreated()? – Saad Jun 20 '14 at 10:47
  • i dont think so..may be another exception..post the logact – kalyan pvs Jun 20 '14 at 10:48
  • you mean it shouldn't crash? – Saad Jun 20 '14 at 10:50
  • @Saad extend FragmentActivity instead of Activity..i have edited change like that – kalyan pvs Jun 20 '14 at 10:51
  • your answer worked for me just to be clear. but initiating the textview in onactivityCreated crashes even if I extends to FragmentActivity. Is it because the Fragment is associated with two activities? where as only one activity can be active at a time? can this be the reason – Saad Jun 20 '14 at 10:59
  • Is your TextView is inside Activity Layout or Fragment Layout?? – kalyan pvs Jun 20 '14 at 11:00
  • 06-20 07:06:48.913: E/AndroidRuntime(3244): FATAL EXCEPTION: main 06-20 07:06:48.913: E/AndroidRuntime(3244): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmentflexibleui2/com.example.fragmentflexibleui2.OtherActivity}: java.lang.NullPointerException – Saad Jun 20 '14 at 11:07
  • So its NUllPointer..Are you importing classes from Support library – kalyan pvs Jun 20 '14 at 11:09
  • minSDK is 14. Do I still need support? – Saad Jun 20 '14 at 11:11
  • not SDK..Fragment imports check this http://stackoverflow.com/questions/6424853/error-inflating-class-fragment – kalyan pvs Jun 20 '14 at 11:13
  • Hey @kalyan thanks a ton. I got the solution if I had problem I'll post again. Meanwhile I am good – Saad Jun 20 '14 at 11:17
1

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);
Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86