我想从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
答案 0 :(得分:2)
尝试使用getActivity().startActivity()
替换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;
}
希望这有帮助!
答案 1 :(得分:1)
你必须写下面的代码行来从片段开始活动:
Intent intent = new Intent(getActivity(), ActivityB.class);
startActivity(intent);
答案 2 :(得分:1)
public FindPeopleFragment(){}
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
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);
view.getContext().startActivity(intent1);
break;
}
}
});
return view;
}
答案 3 :(得分:0)
如何使用片段内的按钮启动另一个活动
此答案最适合android中的Navigation Drawble
清单
//设置新Java类的路径,该Java类在片段类的Intent下定义
<activity android:name="com.example.mysqlinsert.ui.settings.setLayouts"></activity>
在片段类中使用Intent的类
public class setLayouts extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.about);
//layout name which you need to set using onClickListner
}
}
片段Java类
public class SettingFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate( R.layout.fragment_layoutName, container, false );
Button about = (Button) root.findViewById(R.id.about);
about.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent( getContext(), requiredJavaClassName.class );
startActivity( intent );
}
} );
return root;
}
}````