【问题标题】:This error occur when clicking on button in fragment单击片段中的按钮时发生此错误
【发布时间】:2016-12-08 05:59:48
【问题描述】:

我正在使用选项卡活动并添加片段和活动。现在我想通过单击片段中的按钮来打开活动,当我单击按钮时应用程序崩溃错误发生应用程序停止不幸,这不起作用。

我正在使用此代码:

public class SmsList extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootview = inflater.inflate(R.layout.fragment_sms_list, container, false);
        Button button1 = (Button)rootview.findViewById(R.id.btinboxsms);
        Button button2 = (Button)rootview.findViewById(R.id.btsetetime);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                Intent i = new Intent(getActivity(), InboxSms.class);

                startActivity(i);

            }
        });

        return rootview;
    }
}

java.lang.IllegalStateException:无法在父或祖先上下文中找到方法 setListFooter(View) 为 android:onClick 属性定义在视图类 android.support.v7.widget.AppCompatButton 上,id 为“btinboxsms”

【问题讨论】:

  • 分享错误日志,希望你在 xml 中做错了
  • 您是否在清单中定义了活动?
  • 尝试使用 onclick 监听器移除 view.Onclicklistener : button1.setOnClickListener(new OnClickListener() {}
  • java.lang.IllegalStateException:无法在父或祖先上下文中找到方法 setListFooter(View) 为 android:onClick 属性定义在视图类 android.support.v7.widget.AppCompatButton 与 id 'btinboxsms '

标签: android android-studio


【解决方案1】:

试试这个

this.getActivity.startActivity(this.getApplicationContext(), InboxSms.class);

【讨论】:

    【解决方案2】:
     Context context=rootview.getContext();
     Intent i = new Intent(context, InboxSms.class);
     startActivity(i);
    

    【讨论】:

    • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
    • 谢谢兄弟,我是 Stackoverflow 的新手,以后会继续。
    【解决方案3】:

    您必须在单击片段中的按钮时调用 Intent。 试试下面的代码希望这对你有用。

    public class AlertFragment extends android.support.v4.app.Fragment{
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
    abcBtnn.setOnClickListener(new View.OnClickListener() {
     @Override
            public void onClick(View view) {
    Intent intent = new Intent(getActivity(), SecondActivity.class);
    startActivity(intent);
    }
    });
    }
    

    附言 您必须在您的应用 Menifest 中声明您的活动

    【讨论】:

    • java.lang.IllegalStateException:无法在父或祖先上下文中找到方法 setListFooter(View) 为 android:onClick 属性定义在视图类 android.support.v7.widget.AppCompatButton 与 id 'btinboxsms '
    • 请从您的 XML 文件中删除您的 android:onClick
    • 如果你想使用“android:onClick”所以请不要使用Listener
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多