[英]Starting an AppCompatActivity Activity from a class that extends Fragment
I have been through a lot of posts here about starting an activity from a fragment, although none of them seem to be solving my problem.我在这里阅读了很多关于从片段开始活动的帖子,尽管它们似乎都没有解决我的问题。 As i say, i am trying to start an activity from a class that extends a fragment.
正如我所说,我试图从一个扩展片段的类开始一个活动。 At the minute, when i remove the "android:onClick" from the xml, the button simply becomes dead and it does not let me click it.
此刻,当我从 xml 中删除“android:onClick”时,按钮只是变得死了,它不允许我点击它。 However when i put in the OnClick method to the xml i receive the following error:
但是,当我将 OnClick 方法放入 xml 时,我收到以下错误:
java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'buttonViewDetails' at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) at android.view.View.performClick(View.java:5653) at android.view.View$PerformClick.run(View.java:22509) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6144) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
java.lang.IllegalStateException:在 android.support.v7.widget.AppCompatButton 的 android.support.v7.widget.AppCompatButton 上定义的 android:onClick 属性的父级或祖先上下文中找不到方法 onClick(View),id 为 android.support.v7。 app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) at android.view.View.performClick(View3.java:56)在 android.view.View$PerformClick.run(View.java:22509) 在 android.os.Handler.handleCallback(Handler.java:751) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android。 os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6144) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os。 ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
I am not quite sure what this means.我不太确定这意味着什么。 Here is the code that i have tried:
这是我尝试过的代码:
Button XML inside the class extending fragment:类扩展片段内的按钮 XML:
<Button
android:id="@+id/buttonViewDetails"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@mipmap/info"
android:layout_marginLeft="250dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="14dp"
android:layout_marginBottom="14dp" />
Java Code for the class that extends fragment:扩展片段的类的 Java 代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myInflatedView = inflater.inflate(R.layout.line_details, container, false);
Bundle extras = getArguments();
if (extras != null) {
String linetype = extras.getString("LineType");
TextView LT = (TextView) getActivity().findViewById(R.id.textViewLineType);
LT.setText(linetype);
Button InfoButton = (Button) getActivity().findViewById(R.id.buttonViewDetails);
InfoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), VerifyLine.class);
startActivity(intent);
}
});
}
return myInflatedView;
}
Would anyone be able to help me out?有人能帮我吗?
Thanks!谢谢!
You are inflating a view myInflatedView and doing findViewById of that view using activity reference.So ust change that getActivity and use myInflatedView to find the Views.Also remove onClick for the button from xml.您正在膨胀视图 myInflatedView 并使用活动引用执行该视图的 findViewById。因此,必须更改该 getActivity 并使用 myInflatedView 查找视图。同时从 xml 中删除按钮的 onClick。
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myInflatedView = inflater.inflate(R.layout.line_details, container, false);
TextView LT = (TextView) myInflatedView.findViewById(R.id.textViewLineType);
Button InfoButton = (Button) myInflatedView.findViewById(R.id.buttonViewDetails);
InfoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), VerifyLine.class);
startActivity(intent);
}
});
Bundle extras = getArguments();
if (extras != null) {
String linetype = extras.getString("LineType");
LT.setText(linetype);
}
return myInflatedView;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.