1

I have a problem with calling activity from fragment class. More:

XML of adapter's layout:

<TextView
     style="@style/LiHeadLogin"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:foreground="?android:attr/selectableItemBackground"
     android:clickable="true"
     android:onClick="openProfile"
     android:id="@+id/genFrom" />

Code from main activity that calls fragment:

public void openProfile(View v) {
     Fragment_Questions frau = new Fragment_Questions();
     frau.openProfile(v);
}

Code from fragment class:

public void openProfile(View v) {

    View row = (View) v.getParent();

    TextView child2 = (TextView) row.findViewById(R.id.genFromlogin);
    String child3 = child2.getText().toString();

    Intent ini = getActivity().getIntent();
    String c_username = ini.getStringExtra(MainActivity.KEY_USERNAME);
    String c_password = ini.getStringExtra(MainActivity.KEY_PASSWORD);
    Intent ini2 = new Intent(context, User.class);
    ini2.putExtra(MainActivity.KEY_USERNAME, c_username);
    ini2.putExtra(MainActivity.KEY_PASSWORD, c_password);
    ini2.putExtra(MainActivity.KEY_USER, child3);
    getActivity().startActivity(ini2);

}

Errors:

java.lang.IllegalStateException: Could not execute method for android:onClick
...
...
...
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent android.support.v4.app.FragmentActivity.getIntent()' on a null object reference

P.S. 1. App opens default activity 2. User calls a new activity with passing intent data 3. New activity calls fragment

The first (main) activity:

Intent intent_settings = getIntent();
            String c_username = intent_settings.getStringExtra(MainActivity.KEY_USERNAME);
            String c_password = intent_settings.getStringExtra(MainActivity.KEY_PASSWORD);
            Intent intent_settings_1 = new Intent(this, NewFeed.class);
            intent_settings_1.putExtra(MainActivity.KEY_USERNAME, c_username);
            intent_settings_1.putExtra(MainActivity.KEY_PASSWORD, c_password);
            intent_settings_1.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(intent_settings_1);

Okay, and you should know that all fragments are called by viewpager with tab layout.

@Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                Fragment_Home tab1 = new Fragment_Home();
                return tab1;
            case 1:
                Fragment_Questions tab2 = new Fragment_Questions();
                return tab2;
            default:
                return null;
        }
    }
HEISENBERG
  • 29
  • 1
  • 10

5 Answers5

1

You can only access the activity with getActivity() between calls to onAttach() and onDetach().

Fragment_Questions itself only seems to serve the purpose to start a new activity, so you could just move all the logic to the click listener of the activity and don't use the fragment at all.

If there's more about the fragment and you're actually planning to add it to the activity sometime in the future it still seems better to have this logic separated from the fragment. You only use getActivity() and magically context in there. You could just make it static and give the activity as parameter instead.

public static void openProfile(Activity activity, View v) {
    View row = (View) v.getParent();

    TextView child2 = (TextView) row.findViewById(R.id.genFromlogin);
    String child3 = child2.getText().toString();

    Intent ini = activity.getIntent();
    String c_username = ini.getStringExtra(MainActivity.KEY_USERNAME);
    String c_password = ini.getStringExtra(MainActivity.KEY_PASSWORD);
    Intent ini2 = new Intent(activity, User.class);
    ini2.putExtra(MainActivity.KEY_USERNAME, c_username);
    ini2.putExtra(MainActivity.KEY_PASSWORD, c_password);
    ini2.putExtra(MainActivity.KEY_USER, child3);
    activity.startActivity(ini2);
}
tynn
  • 38,113
  • 8
  • 108
  • 143
0

Do you extends Fragment OR FragmetnActivity?

if you extend FragmetnActivity (as i think) then dont use getActivity()

Also add the code for the intent that started your current activity

 Fragment_Questions frau = new Fragment_Questions();
     frau.openProfile(v);

You just created new fragment you didnt attached it to the activity? use FragmentManager.

if you trying to use fragment inside ViewPager use THIS guide

Community
  • 1
  • 1
yotam hadas
  • 702
  • 3
  • 14
0

You should add Fragment_Questions to Activity.

For example,In Activity layout:

 <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"
     tools:context=".MainActivity" >

     <fragment
         android:id="@+id/fragment_question"
         android:name="com.example.view.Fragment_Questions"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
 </LinearLayout>

In Acitivity.java:

questionFragment = (Fragment_Questions) findViewById(fragment_question);

and then, you can user OnClick Event like this:

questionFragment.openProfile(v);
Zhang Ligao
  • 147
  • 1
  • 2
  • 10
0

You are getting Null Pointer Exception because your fragment is not attached to Activity. You are just instantiating the fragment and then calling its openProfile(View v) method which is a wrong way to handle events. If you already attached fragment then get the instance of framgent using

questionFragment = (Fragment_Questions) findViewById(fragment_question);

You can read below article on how to communicate from Fragments to activity https://developer.android.com/training/basics/fragments/communicating.html

Passiondroid
  • 1,573
  • 1
  • 16
  • 28
0

As I have understanded the steps of the user interferance you have to replace getIntent() in the fragment with the intent that starts it which is intent_settings i think

Also make the that intent static in order to share it

And make if statment in order not to have null exception

if(theIntent.hasPutextra)
{getExtra.....}
Dasser Basyouni
  • 3,142
  • 5
  • 26
  • 50