1

I'm new to Android dev, and my app crashes when I try to create a new fragment upon clicking my button that has an "onClick" that is called "EnterNameOnClick()" I suspect that the following code from MainActivity might be the problem...

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void EnterNameOnClick(){
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        EnterName_Fragment enterName_fragment = new EnterName_Fragment();
        fragmentTransaction.add(android.R.id.content, enterName_fragment);
        fragmentTransaction.commit();
    }
}

public class EnterName_Fragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.new_user_fragment, container, false);
        Spinner spinner = (Spinner) view.findViewById(R.id.movie_spinner);

        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getActivity(), R.array.planets_array, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinner.setAdapter(adapter);

        return view;
    }
}

When I am writing the fragmentTransaction.add(android.R.id.content, enterName_fragment), I wonder if the "android.R.id.content" is erroneous? What am I supposed to fill this field with?

Mike
  • 4,550
  • 4
  • 33
  • 47
Jasmine Rain
  • 419
  • 2
  • 6
  • 17
  • Can you post your logcat? – Anirudh Murali Jan 22 '16 at 15:38
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Jan 22 '16 at 15:38
  • FATAL EXCEPTION: main java.lang.IllegalStateException: Could not find method EnterNameOnClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'enter_name' – Jasmine Rain Jan 22 '16 at 15:46
  • Instead of using onClick in xml, try explicitly. See my answer. – Anirudh Murali Jan 22 '16 at 15:46
  • Hey buddy, thanks very much. I just added "View view" within the parameters of public void EnterNameOnClick(View view), and now it's working. Thanks.. – Jasmine Rain Jan 22 '16 at 15:49

1 Answers1

0

Maybe this is caused because using android:onclick in xml. Try onClick in Java using onClickListener

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {

    public void EnterNameOnClick(View v) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        EnterName_Fragment enterName_fragment = new EnterName_Fragment();
        fragmentTransaction.add(android.R.id.content, enterName_fragment);
        fragmentTransaction.commit();
    }
});

Posting your logcat would help us to provide better solution. Try it and let me know.

Mike
  • 4,550
  • 4
  • 33
  • 47
Anirudh Murali
  • 612
  • 10
  • 25