gpt4 book ai didi

java - 从 Fragment 更改为 Activity

转载 作者:太空宇宙 更新时间:2023-11-04 14:21:01 25 4
gpt4 key购买 nike

我是 Android 编程新手。我有一个问题,我如何才能将 fragment Intent 转化为 Activity 。我试图对其进行编码,但失败了:((“l_places”是 Fragment Places 的布局,“p_tr_fr”是可点击的 TableRow)
这是我的 fragment 代码:

package com.fragment.toactivity;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class Places extends Fragment{


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.l_places, container, false);//l_places is the layout
return rootView;
}

public void goToAttract(View v)
{
if (v.getId()== R.id.p_tr_fr) {
Intent intent = new Intent(getActivity(), Listact.class);
startActivity(intent);
}
}
public View onCreateView(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

};
}

这是我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffd4d4d4">

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollView2" >

<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="#ffffffff">

<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_gravity="bottom"
style="@android:style/Widget.AutoCompleteTextView"
android:layout_margin="0dp"
android:id="@+id/p_tr_fr"
android:onClick="onClick">

<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="@+id/imageView2"
android:src="@drawable/m_pf"
android:scaleType="fitCenter"
android:layout_marginLeft="6dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/s_shop"
android:id="@+id/textView3"
android:layout_column="1"
android:layout_gravity="center_vertical"
android:textStyle="bold"
android:textSize="18dp"
android:textColor="#ff000000"
android:layout_marginLeft="6dp" />
</TableRow>
</TableLayout>
</ScrollView>

</LinearLayout>

当我单击 TableRow 时,应用程序停止。请帮忙!(抱歉我的英语)

最佳答案

这样做,

Intent myIntent = new Intent(getActivity(), BookmarkActivity.class);
getActivity().startActivity(myIntent);

关于java - 从 Fragment 更改为 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27202720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com