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.
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.
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