gpt4 book ai didi

java - 在 fragment 中链接两个按钮

转载 作者:行者123 更新时间:2023-12-02 13:42:20 24 4
gpt4 key购买 nike

我在将两个按钮从 fragment 链接到类时遇到问题。我有一个正在工作,但任何将第二个链接到不同类的尝试都失败了。这是我的一个按钮的代码。提前致谢!

@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;
}

更新:我收到了一些很好的回复,但是当我单击帐户 fragment 时,我的应用程序崩溃了,我不知道为什么。更新后的代码如下:

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) {

}
}

我收到以下错误日志

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)

再次感谢您的帮助!

最佳答案

我认为问题在于当你这样做时

Button button = (Button)view.findViewById(R.id.btGlobex);

此按钮始终会链接到 R.id.btGlobex,因此您的 v.getId() switch case 始终会获取 R.id.btGlobex 的 id

如果您希望第二个按钮使用不同的类启动 Intent ,则需要通过第二个按钮上的 findViewById 执行对第一个按钮所做的操作,并向其添加一个 onClickListener 来启动第二个类,或者实现我认为 onClickListener 接口(interface)是这样的:

public class YourFragment extends Fragment implements View.OnClickListener {

并对 onCreateView 中的按钮执行此操作:

Button button = (Button) view.findViewById(R.id.button1);
button.setOnClickListener(this);

Button button2 = (Button) view.findViewById(R.id.button2);
button2.setOnClickListener(this);

然后将你的 switch case 放在 onCreateView 之外作为它自己的方法:

@Override
public void onClick(View v) {
// Switch cases here
}

参见this answer了解更多信息。

<小时/>

也许你应该像这样设置你的 onClickListener ?

public class YourFragment extends Fragment implements View.OnClickListener {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.account, container, false);

Button btGlobex = (Button) view.findViewById(R.id.btGlobex);
btGlobex.setOnClickListener(this);

Button btUmbrella = (Button) view.findViewById(R.id.btUmbrella);
btUmbrella.setOnClickListener(this);

return view;
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btGlobex:
Intent intent1 = new Intent(getActivity(), GlobexActivity.class);
startActivity(intent1);
break;
case R.id.btUmbrella:
Intent intent2 = new Intent(getActivity(), UmbrellaActivity.class);
startActivity(intent2);
break;
default:
break;
}
}

关于java - 在 fragment 中链接两个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42676412/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com