我试图使用button
转到另一个页面,但它总是失败。
这是我的第一类XML及其XML:
public class FindPeopleFragment extends Fragment {
public FindPeopleFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
public void goToAttract(View v)
{
Intent intent = new Intent(getActivity().getApplication(), MainActivityList.class);
startActivity(intent);
}
}
这是我的XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_marginBottom="48dp"
android:onClick="goToAttract"
android:text="Button" /></RelativeLayout>
这是我的stacktrace ..这是我使用onclicklistener时的结果
12-30 16:54:28.006: E/AndroidRuntime(992): FATAL EXCEPTION: main
12-30 16:54:28.006: E/AndroidRuntime(992): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.slidingmenu/info.androidhive.slidingmenu.MainActivityList}: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
12-30 16:54:28.006: E/AndroidRuntime(992): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-30 16:54:28.006: E/AndroidRuntime(992): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-30 16:54:28.006: E/AndroidRuntime(992): at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-30 16:54:28.006: E/AndroidRuntime(992): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-30 16:54:28.006: E/AndroidRuntime(992): at android.os.Handler.dispatchMessage(Handler.java:99)
12-30 16:54:28.006: E/AndroidRuntime(992): at android.os.Looper.loop(Looper.java:137)
12-30 16:54:28.006: E/AndroidRuntime(992): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-30 16:54:28.006: E/AndroidRuntime(992): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 16:54:28.006: E/AndroidRuntime(992): at java.lang.reflect.Method.invoke(Method.java:511)
12-30 16:54:28.006: E/AndroidRuntime(992): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-30 16:54:28.006: E/AndroidRuntime(992): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-30 16:54:28.006: E/AndroidRuntime(992): at dalvik.system.NativeStart.main(Native Method)
12-30 16:54:28.006: E/AndroidRuntime(992): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
12-30 16:54:28.006: E/AndroidRuntime(992): at android.view.LayoutInflater.createView(LayoutInflater.java:606)
12-30 16:54:28.006: E/AndroidRuntime(992): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
12-30 16:54:28.006: E/AndroidRuntime(992): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
12-30 16:54:28.006: E/AndroidRuntime(992): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
12-30 16:54:28.006: E/AndroidRuntime(992): at android.view.LayoutInflater.rInflate(LayoutInflater.java:
答案 0 :(得分:43)
使用此
public void goToAttract(View v)
{
Intent intent = new Intent(getActivity(), MainActivityList.class);
startActivity(intent);
}
请确保您在Manifest中注册了MainActivityList
答案 1 :(得分:5)
尝试使用此代码 -
public class FindPeopleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home,
container, false);
Button button = (Button) rootView.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateDetail();
}
});
return rootView;
}
public void updateDetail() {
Intent intent = new Intent(getActivity(), MainActivityList.class);
startActivity(intent);
}
}
根据Raghunandan的建议,从fragment_home.xml
-
android:onClick="goToAttract"
答案 2 :(得分:3)
删除此
android:onClick="goToAttract"
然后
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
Button b = (Button)rootView.findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(getActivity(), MainActivityList.class);
startActivity(intent);
}
});
return rootView;
错误说您需要在Activity类<{p>中public void goToAttract(View v)
Could not find method goToAttract(View) in the activity class
原因请查看PareshMayani @
的答案Android app crashing (fragment and xml onclick)
编辑:
Caused by: java.lang.OutOfMemoryError
我猜你的图片太大而无法放入,需要按比例缩小。因此OutOfMemoryError
。
答案 3 :(得分:2)
在您的接收意图中用作
Intent intent = getActivity().getIntent();
((TextView)view.findViewById(R.id.hello)).setText(intent.getStringExtra("Hello"));
和你的发送意图
Intent intent = new Intent(getActivity(),Main2Activity.class);
intent.putExtra("Hello","Nisar");
getActivity().startActivity(intent);
记住两者都是片段
答案 4 :(得分:1)
public class OneWayFragment extends Fragment {
ImageView img_search;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.one_way_fragment, container, false);
img_search = (ImageView) view.findViewById(R.id.search);
img_search.setOnClickListener(new View.OnClickListener() {
@Override`enter code here`
public void onClick(View v) {
Intent displayFlights = new Intent(getActivity(), SelectFlight.class);
startActivity(displayFlights);
}
});
答案 5 :(得分:0)
您需要在片段中使用Intents的getActivity()方法。
Intent intent = new Intent(getActivity(), SecondActivity.class);
startActivity(intent);
答案 6 :(得分:0)
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame, new MySchedule()).commit();
MySchedule是我的java类的名称。
答案 7 :(得分:0)
对于Kotlin,您可以使用...
val myIntent = Intent(activity, your_destination_activity::class.java)
startActivity(myIntent)
答案 8 :(得分:0)
希望这段代码会有所帮助
public class ThisFragment extends Fragment {
public Button button = null;
Intent intent;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.yourlayout, container, false);
intent = new Intent(getActivity(), GoToThisActivity.class);
button = (Button) rootView.findViewById(R.id.theButtonid);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
return rootView;
}
您可以使用此代码,确保将“ ThisFragment ”更改为片段名称,将“ yourlayout ”更改为布局名称“ GoToThisActivity ”将其更改为您想要的活动,然后“ theButtonid ”使用您使用的按钮ID进行更改。
答案 9 :(得分:0)
如果您可以在该片段中获得View
,则可以从那里访问上下文:
view.getContext().startActivity(intent);
答案 10 :(得分:0)
使用 getContext()代替MainActivity。this
Intent intent = new Intent(getContext(), SecondActivity.class);
startActivity(start);
答案 11 :(得分:0)
您可以使用以下任何一种:
Intent i = new Intent(getActivity().getApplication(),SecondActivity.class);
或
Intent i = new Intent(getActivity(),SecondActivity.class);