0

i just want to open SkipActivity from MondayFragment, and fail in error. The swipe between fragments is work, but when i click on skip button move to another Activity- the app is crushed(closed):

All relevant code is attached, please help:

        public class MondayFragment extends Fragment {

            final String LOG_TAG="myLogs";
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                View v=inflater.inflate(R.layout.fragment_monday, container, false);

              TextView txt=(TextView) v.findViewById(R.id.skip);
              txt.setOnClickListener(new View.OnClickListener() {
                  public void onClick(View v) {
                      ((TextView)getActivity().findViewById(R.id.skip)).setText("Access from Monday Fragment");
                      //Start your activity here
                      Intent i = new Intent(getActivity(),SkipActivity.class);
                      startActivity(i);

              }
          });
          return v;

        }

       }

This is java Activity class :

public class SkipActivity extends AppCompatActivity {
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.after_skip_scr);
    }
}

This is MainActivity class - i think there is nothing to change here:

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the content of the activity to use the activity_main.xml layout file
        setContentView(R.layout.activity_main);

        // Find the view pager that will allow the user to swipe between fragments
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);

        // Create an adapter that knows which fragment should be shown on each page
        SimpleFragmentPagerAdapter adapter = new SimpleFragmentPagerAdapter(getSupportFragmentManager());

        // Set the adapter onto the view pager
        viewPager.setAdapter(adapter);


    }


}

Below is res-layout files: The first one is for monday fragment

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/monday_main"
    android:background="#a7cbeb">

    <TextView
        android:id="@+id/welcome_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:layout_centerInParent="true"
        android:textColor="#191970"
        android:text="Welcome to Circles"
        android:textSize="29sp"
        />


    <TextView
        android:id="@+id/skip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="54px"
        android:textStyle="italic"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="25dp"
        android:layout_marginTop="25dp"
        android:text="skip"
        android:textColor="#ffffff"
        android:onClick="openSkipActivity"
        />

</RelativeLayout>

And this one is for main activity:

<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"
    android:orientation="vertical"

    tools:context="MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
MyTwoCents
  • 7,284
  • 3
  • 24
  • 52
Dima Bokov
  • 91
  • 1
  • 14

5 Answers5

0

you can call activity by :

button.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View arg0) {
              Intent intent = new Intent(getActivity(), Contact_Developer.class);
              getActivity().startActivity(intent);
           }
        });
amit
  • 659
  • 1
  • 8
  • 21
  • TextView txt=(TextView) v.findViewById(R.id.skip); txt.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { @Override Intent intent = new Intent(getActivity(), SkipActivity.class); getActivity().startActivity(intent); } Just tried, but it still crashed.... – Dima Bokov Aug 01 '18 at 07:08
0

why are you take this line

((TextView)getActivity().findViewById(R.id.skip)).setText("Access from Monday Fragment");

and share logcat error log.

or try this

decalare this

     Context context;

    txt.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
          Intent intent = new Intent(context, SkipActivity.class);
          context.startActivity(intent);
       }
    });
  • Hi, tnx for answering. The line ((TextView)getActivity().findViewById(R.id.skip)).setText("Access from Monday Fragment"); doesn't cause any error. I've tried your solution with contex but it still not work...:( – Dima Bokov Aug 01 '18 at 07:04
0

Your app is crashing on click event because your view is null the reason is you are trying to find out the view by using activity.findViewById which will obvious return null.

((TextView)getActivity().findViewById(R.id.skip)).setText("Access from Monday Fragment");

Change above line with this

((TextView)v.findViewById(R.id.skip)).setText("Access from Monday Fragment");

After all you are launching activity from onClick event so doesn't make sense to update the text of skip textview. but if still you want to do that, no problem.

Just replace the above line of code, it should fix your issue.

As a suggestion in case you want to update the skip textview text in on click event or any other place, create a class level instance say TextView skipTextView and initialize in onCreateView then simply use skipTextView.setTex("<text>")" all over the place where ever required.

Krishna Sharma
  • 2,828
  • 1
  • 12
  • 23
  • ((TextView)getActivity().findViewById(R.id.skip)).setText("Access from Monday Fragment"); This line doesn't cause to any problem. I just putted it in comment for now- the line is unnecessary. – Dima Bokov Aug 01 '18 at 07:07
  • @DimaBokov please share crash log, will help us to find out the cause – Krishna Sharma Aug 01 '18 at 07:12
  • **I hope you have delcared your activity `SkipActivity` in manifest file, if not then you need to do that.** – Krishna Sharma Aug 01 '18 at 07:14
0

@Dima , Try this :

public class MondayFragment extends Fragment implements OnClickListener
{

        final String LOG_TAG="myLogs";
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.fragment_monday, container, false);

        // Views
        TextView txt=(TextView) v.findViewById(R.id.skip);
        txt.setOnClickListener(this);

        return v;

        }

    // onClick Method
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.skip:
                ((TextView)getActivity().findViewById(R.id.skip)).setText("Access from Monday Fragment");
                Intent i = new Intent(getActivity(),SkipActivity.class);
                startActivity(i);
                break;
            default:
                break;
        }


    }
}
Siros Baghban
  • 406
  • 4
  • 7
0

tnx for answers! The problem was - i didn't declare the SkipActivity in manifest file.

At the moment i did it- the app is running and i can swipe between fragments and launch another Activity from fragment.

tnx a lot!

Dima Bokov
  • 91
  • 1
  • 14