如何使用Fragment中的Button启动另一个Activity(How to start another Activity using Button inside the Fragment)

编程入门 行业动态 更新时间:2024-10-18 01:38:49
如何使用Fragment中的Button启动另一个Activity(How to start another Activity using Button inside the Fragment)

我想从Fragment Button打开另一个活动,但每次我运行应用程序时,我都会不幸停止。

我已经这样做了一个星期仍然无法运行按钮。 如果有人给我回答,我将不胜感激。 我是android的新手。

我的代码是侧边菜单,然后其中一个侧边菜单包含用于打开新活动的按钮

package tipqc.softeng.sundroidtracker; import android.app.Fragment; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; public class FindPeopleFragment extends Fragment{ View view; //Intent i; public FindPeopleFragment(){} @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View view = inflater.inflate(R.layout.fragment_find_people, container, false); Button button = (Button)view.findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v){ switch(v.getId()){ case R.id.button1: Intent intent1 = new Intent(view.getContext(), ResearchStudy.class); startActivity(intent1); break; } //startActivity(i); } }); return view; } }

下面是日志

12-31 14:36:33.040: E/AndroidRuntime(2843): FATAL EXCEPTION: main 12-31 14:36:33.040: E/AndroidRuntime(2843): Process: tipqc.softeng.sundroidtracker, PID: 2843 12-31 14:36:33.040: E/AndroidRuntime(2843): java.lang.RuntimeException: Unable to start activity ComponentInfo{tipqc.softeng.sundroidtracker/tipqc.softeng.sundroidtracker.ResearchStudy}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.app.ActivityThread.access$800(ActivityThread.java:144) 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.os.Handler.dispatchMessage(Handler.java:102) 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.os.Looper.loop(Looper.java:135) 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.app.ActivityThread.main(ActivityThread.java:5221) 12-31 14:36:33.040: E/AndroidRuntime(2843): at java.lang.reflect.Method.invoke(Native Method) 12-31 14:36:33.040: E/AndroidRuntime(2843): at java.lang.reflect.Method.invoke(Method.java:372) 12-31 14:36:33.040: E/AndroidRuntime(2843): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 12-31 14:36:33.040: E/AndroidRuntime(2843): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 12-31 14:36:33.040: E/AndroidRuntime(2843): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:110) 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57) 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:99) 12-31 14:36:33.040: E/AndroidRuntime(2843): at tipqc.softeng.sundroidtracker.ResearchStudy.onCreate(ResearchStudy.java:12) 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.app.Activity.performCreate(Activity.java:5933) 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 12-31 14:36:33.040: E/AndroidRuntime(2843): ... 10 more

I want to open another activity from Fragment Button but every time I Run the app i will unfortunately Stop.

I've been doing this for a week still can't run the buttons. I will be appreciate if someone gave me answer. I'm new here in android.

My code is about side menu then one of the side menu contains buttons to open a new activity

package tipqc.softeng.sundroidtracker; import android.app.Fragment; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; public class FindPeopleFragment extends Fragment{ View view; //Intent i; public FindPeopleFragment(){} @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View view = inflater.inflate(R.layout.fragment_find_people, container, false); Button button = (Button)view.findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v){ switch(v.getId()){ case R.id.button1: Intent intent1 = new Intent(view.getContext(), ResearchStudy.class); startActivity(intent1); break; } //startActivity(i); } }); return view; } }

Below is log

12-31 14:36:33.040: E/AndroidRuntime(2843): FATAL EXCEPTION: main 12-31 14:36:33.040: E/AndroidRuntime(2843): Process: tipqc.softeng.sundroidtracker, PID: 2843 12-31 14:36:33.040: E/AndroidRuntime(2843): java.lang.RuntimeException: Unable to start activity ComponentInfo{tipqc.softeng.sundroidtracker/tipqc.softeng.sundroidtracker.ResearchStudy}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.app.ActivityThread.access$800(ActivityThread.java:144) 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.os.Handler.dispatchMessage(Handler.java:102) 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.os.Looper.loop(Looper.java:135) 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.app.ActivityThread.main(ActivityThread.java:5221) 12-31 14:36:33.040: E/AndroidRuntime(2843): at java.lang.reflect.Method.invoke(Native Method) 12-31 14:36:33.040: E/AndroidRuntime(2843): at java.lang.reflect.Method.invoke(Method.java:372) 12-31 14:36:33.040: E/AndroidRuntime(2843): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 12-31 14:36:33.040: E/AndroidRuntime(2843): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 12-31 14:36:33.040: E/AndroidRuntime(2843): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:110) 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57) 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:99) 12-31 14:36:33.040: E/AndroidRuntime(2843): at tipqc.softeng.sundroidtracker.ResearchStudy.onCreate(ResearchStudy.java:12) 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.app.Activity.performCreate(Activity.java:5933) 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 12-31 14:36:33.040: E/AndroidRuntime(2843): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 12-31 14:36:33.040: E/AndroidRuntime(2843): ... 10 more

最满意答案

尝试使用startActivity()替换getActivity().startActivity()方法,它会起作用!

以下是完整的代码:

public FindPeopleFragment(){} @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_find_people, container, false); Button button = (Button)view.findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v){ switch(v.getId()){ case R.id.button1: Intent intent1 = new Intent(getActivity(), ResearchStudy.class); startActivity(intent1);//Edited here break; } } }); return view; }

希望这可以帮助!

Try replacing getActivity().startActivity() method, with startActivity() and it would work!.

Below is the complete code :

public FindPeopleFragment(){} @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_find_people, container, false); Button button = (Button)view.findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v){ switch(v.getId()){ case R.id.button1: Intent intent1 = new Intent(getActivity(), ResearchStudy.class); startActivity(intent1);//Edited here break; } } }); return view; }

Hope this helps!

更多推荐

android,AndroidRuntime,java,电脑培训,计算机培训,IT培训"/> <meta name="d

本文发布于:2023-04-28 03:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329748.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   Fragment   Button   Activity   start

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!