调用片段的Java活动
首先,请允许我声明我对Java和Android Studio相当陌生,所以请容忍我。我的背景是VisualBasic 我有一个活动试图验证UPC输入的是一个有效的项目编号。当只有一场比赛时,一切都好。当输入的UPC带回多个匹配项时,我的问题就开始了。我试图做的(第一次使用片段)是用显示匹配项目和描述的回收器视图替换屏幕的一部分 当返回多个项目时,我使用以下代码调用名为setupMulitpleItems的事件:调用片段的Java活动,java,android,android-fragments,Java,Android,Android Fragments,首先,请允许我声明我对Java和Android Studio相当陌生,所以请容忍我。我的背景是VisualBasic 我有一个活动试图验证UPC输入的是一个有效的项目编号。当只有一场比赛时,一切都好。当输入的UPC带回多个匹配项时,我的问题就开始了。我试图做的(第一次使用片段)是用显示匹配项目和描述的回收器视图替换屏幕的一部分 当返回多个项目时,我使用以下代码调用名为setupMulitpleItems的事件: public void setupMultiItems(String mScan){
public void setupMultiItems(String mScan){
Timber.d("Multiple matching Items");
Bundle bundle = new Bundle();
bundle.putString("valuesArray",mScan);
SelectItemFragment fragobj = new SelectItemFragment();
fragobj.setArguments(bundle);
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.include2, fragobj);
fragmentTransaction.commit();
不确定这段代码中有多少部分可以实际工作,但我在replace行中得到了下划线
"'replace(int, android.app.Fragment)' in 'android.app.FragmentTransaction' cannot be applied to '(int, com.procatdt.stockright.activities.SelectItemFragment)'"
meesage.
我将以下导入用于片段
import android.app.FragmentTransaction;
import android.app.Fragment;
import android.app.FragmentManager;
我想在我想使用Fragment时,我还将包含当前正在运行的活动的XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackgroundGray"
tools:layout="@layout/fragment_select_item"
tools:context="com.procatdt.stockright.activities.PutAwayAreaActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<include
android:id="@+id/include2"
layout="@layout/frm1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="left" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:orientation="vertical">
<include
android:id="@+id/include"
layout="@layout/layout_numpad5"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="220dp"
android:layout_weight="1"
android:layout_gravity="right"
/>
</LinearLayout>
</RelativeLayout>
我试图用Fragment替换include2。
希望这些信息足够多,但如果有人需要查看更多信息,请告诉我,我将附加代码。您只需将片段的对象传递给
replace
方法,无需传递类声明和类类型
所以就把它拿走吧
// this is enough
fragmentTransaction.replace(R.id.include2, fragobj);
// remove this
//fragmentTransaction.replace(R.id.include2,android.app.Fragment SelectItemFragment);
对不起,我忘在里面了。即使这一行仍然收到相同的消息。您需要使用
Framelayout
的id,而不是R.id.include2,
include tagpavneet,我明白您的意思,但我现在尝试了几种不同的方法,没有成功。你能看到那一行应该读什么吗?你的frm1
布局中是否有fragmelayou,如果是,则使用framelayout标记的id而不是include标记,如果仍然存在问题,则发布xml代码并完成错误详细信息,包括fragobj,错误为“replace”(int,android.app.Fragment)'in'android.app.FragmentTransaction'不能应用于'(int,com.procatdt.stockright.activities.SelectItemFragment)'SelectItemFragment是片段活动的名称。我把碎片变成了一种活动。