Luke Luke - 4 months ago 20
Java Question

Starting new Activity from a Fragment with a button

I'm new to Java and android and I'm having some issues.

I'm using navigation drawer activity with each menu item linked to a fragment. From the fragment, I'm trying to setup another menu using buttons that link to more Activities.

Here is my fragment that I want to link to more activities:

package com.notimportant


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

/**
* A simple {@link Fragment} subclass.
*/
public class About extends Fragment {




public About() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_about, container, false);
}

Button button = (Button) rootView.findViewById(R.id.buttonDevs);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), DevsActivity.class);
startActivity(intent);
}
});

}


I'm getting these errors:

Error:(35, 30) error: <identifier> expected
Error:(35, 31) error: illegal start of type
Error:(35, 34) error: ')' expected
Error:(35, 39) error: ';' expected
Error:(35, 40) error: invalid method declaration; return type required
Error:(37, 28) error: ';' expected
Error:(37, 38) error: ';' expected
Error:(41, 6) error: illegal start of type



rootView is highlighted red "Cannot resolve symbol rootView"

setOnClickListener is highlighted red "Cannot resolve symbol
setOnClickListener"

@Override is highlighted red... "Annotations are not allowed here"

view is highlighted red "Cannot resolve symbol view"


My button id is
buttonDevs
. The activity I want to go to is called
DevsActivity
.

What am I doing wrong?

Answer

You have to put Button inside rootView.

Change this

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_about, container, false);
    }

    Button button = (Button) rootView.findViewById(R.id.buttonDevs);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), DevsActivity.class);
            startActivity(intent);
        }
    });

to this

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

      View rootView = inflater.inflate(R.layout.fragment_about, container, false);

     Button button = (Button) rootView.findViewById(R.id.buttonDevs);
     button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), DevsActivity.class);
            startActivity(intent);
        }
    });

        return rootView;
    }
Comments