-1

What is the syntax for a button in a fragment that when is clicked it would go to a listview activity?

Any response would be appreciated. Thanks.

Mae
  • 1
  • 2
  • Is the fragment housed inside the ListActivity? Please post any code you have of the ListActivity and fragment. – PPartisan Apr 14 '15 at 19:14
  • http://pastebin.com/jhf1TntZ This is my coding for the fragment where I want the button. – Mae Apr 14 '15 at 20:44
  • http://pastebin.com/VjK3FRib This is where it should direct after click. The first code is a swipeview activity. – Mae Apr 14 '15 at 20:48

1 Answers1

1

In the code your provided in the comments above:

    public class Exerfrag extends Fragment{
    private Button button1;
    Context context;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.exer,     container, false);

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

    @Override
    public void onClick(View v) {
            Intent intent=new Intent(context, Listview.class);
            startActivity(intent);
    }
    });
return view;
}
}

context is never initialised. Use getActivity() instead so that your onClick method looks like this:

    button1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
            Intent intent=new Intent(getActivity(), Listview.class);
            startActivity(intent);
    }
    });

Edit:

Your second Activity also appears problematic:

public class Listview extends Activity {
        ExpandableListView exv;

        public Listview() {
                // TODO Auto-generated constructor stub
                exv=(ExpandableListView)findViewById(R.id.expandableListView1);
                exv.setAdapter(new MyAdapter(this));
        }

}

You never call onCreate(), inside which you should place the rest of your set-up code, includingsetContentView(R.layout.-) to establish your layout.

Edit 2:

public class Listview extends Activity {
        ExpandableListView exv;

        protected void onCreate(android.os.Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.listview);

        };


        public Listview() {
                // TODO Auto-generated constructor stub
                exv=(ExpandableListView)findViewById(R.id.expandableListView1);
                exv.setAdapter(new MyAdapter(this));
        }

The following lines:

exv=(ExpandableListView)findViewById(R.id.expandableListView1);
                exv.setAdapter(new MyAdapter(this));

Need to be placed inside onCreate(). The section that reads like this:

public Listview(){
    ....
}

is called a constructor. It is actually not required at all in this case, so my advice would be to delete it entirely so that your entire class looks like this:

public class Listview extends Activity {
            ExpandableListView exv;

            protected void onCreate(android.os.Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.listview);
                    exv=(ExpandableListView)findViewById(R.id.expandableListView1);
                    exv.setAdapter(new MyAdapter(this));
               }

This is fairly basic stuff when it comes to Android, so I would perhaps suggest taking a look at the developer docs for a sample Activity class, and a site like Vogella

PPartisan
  • 8,173
  • 4
  • 29
  • 48
  • @Mae Your second Activity never calls `onCreate()`. Take a look at my edit. – PPartisan Apr 14 '15 at 21:04
  • Im sorry, Im still learning how to do things. Do you mind checking what Ive done? http://pastebin.com/F6YvQ2pf – Mae Apr 14 '15 at 21:32