0

I was having a problem before where my application would crash due to the fourth tab being a nested fragment, so I moved the methods to the main activity to be called on when that tab was selected but now it's giving me the error that the onviewcreated method is never used.

Here is my main activity class:

package com.example.hp_user.shoutfinal28;


import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class MainActivity extends FragmentActivity implements OnMapReadyCallback {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from activity_main.xml
    setContentView(R.layout.activity_main);

    // Locate the viewpager in activity_main.xml
    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

    // Set the ViewPagerAdapter into ViewPager
    viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));

}

public void btnShout(View v) {
    //allows for label to be changed to shouted once button is pressed
    EditText txtInput = (EditText) findViewById(R.id.txtInput);
    TextView lblShout = (TextView) findViewById(R.id.lblShout);
    lblShout.setText("Shouted! ");

    //allows for toast to be displayed once button is clicked
    Toast toast = new Toast(getApplicationContext());
    toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0);
    toast.makeText(MainActivity.this, txtInput.getText() + " Has Been Shouted.", toast.LENGTH_SHORT).show();

}

@Override
public void onViewCreated(View v_maps, Bundle savedInstanceState) {
    super.onViewCreated(v_maps, savedInstanceState);
    SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.Maps1);
    if (supportMapFragment!= null) {
        supportMapFragment.getMapAsync(this);
    }
}

@Override
public void onMapReady (GoogleMap map) {
    map.addMarker(new MarkerOptions()
            .position(new LatLng(0, 0))
            .title("Marker"));
}
}

Here is my fragmentshouts_maps class:

public class FragmentShouts_Maps extends Fragment  {


public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Get the view from fragment shouts.xml
    View root_view = inflater.inflate(R.layout.fragmentshouts_maps, container, false);

    return root_view;
}



}
  • 3
    try overriding onViewCreated in your Fragment and not in your Activity – Blackbelt Mar 24 '16 at 15:36
  • Look at the documentation for Fragment, and look at the documentation for FragmentActivity. Which one has the onViewCreated() method, and which one doesn't? – Daniel Nugent Mar 24 '16 at 16:19
  • If you're using a Google Map as one of your tabs, see the last section of this answer on how to do it without nested Fragments, which makes it work without crashing: http://stackoverflow.com/a/32579020/4409409 – Daniel Nugent Mar 24 '16 at 16:24
  • @DanielNugent, i implemented your tab class into my class and my main thread is still overworking, even removed a tab that i had so i only had 2 other fragments away from my map fragment and it still crashed – Alexander Rufus Mar 24 '16 at 17:04
  • What does the stack trace show for the crash? @AlexanderRufus – Daniel Nugent Mar 24 '16 at 18:24
  • @DanielNugent okay I've collected the thread data, I'm new to Java so I don't exactly know what I'm looking for – Alexander Rufus Mar 24 '16 at 18:45
  • @AlexanderRufus Read this: http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors Then add the relevant part of the stack trace to your question. – Daniel Nugent Mar 24 '16 at 18:58
  • trying to understand that has completely fried me, i think i have spent too long in front of the pc for one day. thanks for your help though, im just going to remove the maps completely, caused me way too many problems – Alexander Rufus Mar 24 '16 at 19:09

1 Answers1

0

You must @override onViewCreated() in your Fragment not in the Activity i think your error comes from this.

Kristiyan Varbanov
  • 2,439
  • 2
  • 17
  • 37
  • i previously had it set up like this and my app would crash, thats why i tried it like this i even posted a question earlier today stating that problem – Alexander Rufus Mar 24 '16 at 15:51
  • i removed the fragmentshouts_maps tab from my viewpageradapter and my app has stopped crashing so i know that that its literally only that class. @Kristiyan – Alexander Rufus Mar 24 '16 at 16:16