Issue
I'm having an issue linking two buttons from a fragment to a class. I have one working but any attempt to get the second linking to a different class is failing. Here is the code I have for the one Button. Thanks in advance!
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.account, container, false);
Button button = (Button)view.findViewById(R.id.btGlobex);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v){
switch(v.getId()){
case R.id.btGlobex:
Intent intent = new Intent(getActivity(), GlobexActivity.class);
startActivity(intent);//Edited here
break;
}
}
});
return view;
}
UPDATE: I've gotten some great responses but my app crashes when I click on the account Fragment and I can't figure out as to why. The updated Code is as follows:
public class AccountFragment extends Fragment implements View.OnClickListener{
public AccountFragment() {
}
public static AccountFragment newInstance() {
AccountFragment fragment = new AccountFragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.account, container, false);
Button btGlobex = (Button)getView().findViewById(R.id.btGlobex);
btGlobex.setOnClickListener(onClickListener);
Button btUmbrella = (Button)getView().findViewById(R.id.btUmbrella);
btUmbrella.setOnClickListener(onClickListener);
return view;
}
private View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v){
switch(v.getId()){
case R.id.btGlobex:
Intent intent1 = new Intent(getActivity(), GlobexActivity.class);
startActivity(intent1);//Edited here
break;
case R.id.btUmbrella:
Intent intent2 = new Intent(getActivity(), UmbrellaActivity.class);
startActivity(intent2);//Edited here
break;
}
}
};
@Override
public void onClick(View view) {
}
}
I'm receiving the following error log
03-08 20:10:40.974 5458-5458/com.qreceipts.qreceipts E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.qreceipts.qreceipts, PID: 5458
java.lang.NullPointerException
at com.qreceipts.qreceipts.AccountFragment.onCreateView(AccountFragment.java:29)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2189)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:757)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2355)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
Thanks again for any help!
Solution
After seeing the updated code, the problem is clear. In the first case you are creating a new anonymous OnClickListener and assigning it to just your button.
When you add a second button you are assigning "this" to be the new listener, however if your class does not implement OnClickListener and provide an onClick method, then this won't work.
You can create a listener to use, make it a class level variable so you can use it throughout the class. This means you need to define it outside your onCreateView method, as a private member.
private OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v){
switch(v.getId()){
case R.id.btGlobex:
Intent intent1 = new Intent(getActivity(), GlobexActivity.class);
startActivity(intent1);//Edited here
break;
case R.id.btUmbrella:
Intent intent2 = new Intent(getActivity(), UmbrellaActivity.class);
startActivity(intent2);//Edited here
break;
}
}
}
Now your class provides an implementation of the OnClickListener interface, and you can now pass this listener to your buttons.
Button btGlobex = (Button)findViewById(R.id.btGlobex);
btGlobex.setOnClickListener(onClickListener);
Button btUmbrella = (Button)findViewById(R.id.btUmbrella);
btUmbrella.setOnClickListener(onClickListener);
return view;
In your second example, you are passing "this" to the buttons, which isn't working how you are expecting. You are creating an anonymous OnClickListener which is never being used, when you are using the "this" keyword, it's trying to pass in the entire class. Unless your class implements the OnClickListener interface, and provides an implementation of the onClick method, that will not work.
Answered By - dahui
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.