问题
I am Having an Issue Succesfully opening new activities from these multiple buttons. Im new to coding though. can someone help me with this issues..
I used fragments and I cannot to get to work please help. Here is my code so far. Thank you
public class CreditFragment1 extends Fragment {
private static final View View = null;
Button balance;
Button recharge;
Button share;
Button buy;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return (LinearLayout) inflater.inflate(R.layout.credit_frag1_layout,
container, false);
// at the next line below i get "Unreachable code"
balance = (Button) getView().findViewById(R.id.balance_bt);
recharge = (Button) getView().findViewById(R.id.recharge_bt);
share = (Button) getView().findViewById(R.id.share_bt);
buy = (Button) getView().findViewById(R.id.buy_bt);
OnClickListener balanceListener = new OnClickListener() {
public void onClick(View view) {
setContentView(R.layout.balance_layout);
Intent BalanceIntent = new Intent(getActivity(),BalanceActivity.class);
startActivity(BalanceIntent);
}
private void setContentView(int balanceLayout) {
// TODO Auto-generated method stub
}
};
OnClickListener rechargeListener = new OnClickListener() {
public void onClick(View view) {
setContentView(R.layout.recharge_layout);
Intent BalanceIntent = new Intent(getActivity(),RechargeActivity.class);
startActivity(BalanceIntent);
}
private void setContentView(int rechargeLayout) {
// TODO Auto-generated method stub
}
};
OnClickListener shareListener = new OnClickListener() {
public void onClick(View view) {
setContentView(R.layout.share_layout);
Intent BalanceIntent = new Intent(getActivity(),ShareActivity.class);
startActivity(BalanceIntent);
}
private void setContentView(int shareLayout) {
// TODO Auto-generated method stub
}
};
OnClickListener buyListener = new OnClickListener() {
public void onClick(View view) {
setContentView(R.layout.buy_layout);
Intent BalanceIntent = new Intent(getActivity(),BuyActivity.class);
startActivity(BalanceIntent);
}
private void setContentView(int buyLayout) {
// TODO Auto-generated method stub
}
};
balance.setOnClickListener(balanceListener);
recharge.setOnClickListener(rechargeListener);
share.setOnClickListener(shareListener);
buy.setOnClickListener(buyListener);
return View;
}
}
回答1:
Change onCreateView
to
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.credit_frag1_layout,container, false);
balance = (Button)view.findViewById(R.id.balance_bt);
balance.setOnClickListener(this);
// similarly initialize other buttons
return view;
}
Also your class can implement OnClickListener
public class CreditFragment1 extends Fragment implements OnClickListener {
In onCreateView
balance.setOnClickListener(this);
Then override onClick
.
@Override
public void onClikc(View v)
{
switch(v.getId())
{
case R.id.balance_bt :
// balance button clicked
break;
case R.id.recharge_bt
// recharge button clicked :
break;
// similarly for other buttons
}
}
Also i am not sure what you are trying to do with setContentView(R.layout.balance_layout);
in OnClickListener. Remove setContentView(R.layout.balance_layout);
If you need to navigate to a different Activity use startActivtiy(intent)
回答2:
What is it that you are trying to do?
For one thing, the method setContentView(int shareLayout)
inside the onClickListener
won't do anything for you. Specially if you leave it empty.
What I think you should do from what I understood is start the desired activity like so:
OnClickListener shareListener = new OnClickListener() {
public void onClick(View view) {
Intent BalanceIntent = new Intent(getActivity(),ShareActivity.class);
startActivity(BalanceIntent);
}
};
Then, inside the onCreate()
of your ShareActivity
class, put:
setContentView(R.layout.share_layout);
And remove all the setContentView
from each of your onClickListener()
events.
See the Starting another activity tutorial for details.
Hope it helps.
来源:https://stackoverflow.com/questions/20302089/multiple-buttons-in-fragment-class-issue