- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
编辑 4 好吧,我已经将示例代码更改为建议的内容,并进行了空检查。
现在在公共(public)类 MainActivity 中扩展了 AppCompatActivity 我已经
private templateFragment1 fragTest;
然后在 onCreate 中我得到了
final FragmentManager fragMan = getFragmentManager();
fragTest = (templateFragment1) fragMan.findFragmentByTag("1");
onSave.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(fragTest != null) {
String editTestText = fragTest.editTextGetter();
Toast.makeText(getApplicationContext(), editTestText, Toast.LENGTH_SHORT).show();
}
}
});
在 templateFragment1 中我有
privateEditText text;
我已经像这样在 onCreateView 中绑定(bind)了
text = (EditText) getActivity().findViewById(R.id.editTextTest);
然后我有onCreateView下面的方法
public String editTextGetter(){
String value1 = text.getText().toString();
return value1;
}
现在发生的事情..什么都没有。我点击了保存按钮,没有 Toast,也没有错误消息。所以我猜这意味着它返回 null?
编辑 3 这里有几个视频演示了该错误。
https://www.youtube.com/watch?v=w60Vljd2R4M&feature=youtu.be
https://www.youtube.com/watch?v=xqpVGBAHw0w&feature=youtu.be
我知道已经有几个与此类似的问题,但似乎没有一个问题能解决我的具体问题。
在我的主要 Activity 中,我有一个添加 fragment 实例的按钮。每次执行时,它还会为它们分配“1”、“2”等标签。只是为了看看是否可能,我只是尝试从第一个方法调用一个方法。
在我的主要 Activity 中,我有:
// initializing button that will call the fragment's method
Button onSave = (Button) findViewById(R.id.saveButton);
FragmentManager fragMan = getFragmentManager();
// calling my first created fragment
final templateFragment1 fragTest = (templateFragment1) fragMan.findFragmentByTag("1");
// my button that should call the frag's method and then display it as a toast
onSave.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
// here I'm assigning the fragment's method to a string
String editTestText = fragTest.editTextGetter();
// display as toast
Toast.makeText(getApplicationContext(), editTestText, Toast.LENGTH_SHORT).show();
}
});
在我的 fragment 中:
public String editTextGetter(){
EditText text = (EditText) getActivity().findViewById(R.id.editTextTest);
String value1 = text.getText().toString();
return value1;
}
如果重要的话,editTextGetter() 位于 onCreateView 方法下方。
现在发生的只是单击主 Activity 按钮后应用程序崩溃。
编辑:这是我按要求提交的代码:
addDay.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
++fragIdCount;
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
String fragString = Integer.toString(fragIdCount);
templateFragment1 frag2 = new templateFragment1();
fragmentTransaction.add(R.id.templateFragmentLayout, frag2, fragString);
fragmentTransaction.commit();
}
});
如您所见,我从 0 (fragIdCount) 开始递增一个数字,然后将其转换为字符串。然后我将其指定为 fragment 的标签。所以我试图用这个分配的标签来调用我的 fragment 。
编辑 2: 好的,伙计们,这是可以复制+粘贴到新项目中的代码。它复制了我的问题,如果你们能看一下,我将非常感激!
MainActivity.java
公共(public)类 MainActivity 扩展 AppCompatActivity {
int fragIdCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addDay = (Button) findViewById(R.id.addDay);
Button removeDay = (Button) findViewById(R.id.removeDay);
// Here I'm making it so I have an instance of the fragment displayed on create
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
String fragString = Integer.toString(fragIdCount);
templateFragment1 frag2 = new templateFragment1();
fragmentTransaction.add(R.id.templateFragmentLayout, frag2, fragString);
fragmentTransaction.commit();
// My button that adds fragment. Obviously this can quickly generate a bunch of fragment instances
// I try to make that manageable by incrementing a fragment count to use as tags
addDay.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
++fragIdCount;
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
String fragString = Integer.toString(fragIdCount);
templateFragment1 frag2 = new templateFragment1();
fragmentTransaction.add(R.id.templateFragmentLayout, frag2, fragString);
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
}
});
// remove button. same concept as the add button
removeDay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
String fragString = Integer.toString(fragIdCount);
//Fragment f = getFragmentManager().findFragmentByTag(fragString);
if(fragIdCount != 0){
fragmentTransaction.remove(fragmentManager.findFragmentByTag(fragString)).commit();
--fragIdCount;
}
}
});
// initialize Save button (which SHOULD get the value of an editText from whichever
// fragment I specify)
Button onSave = (Button) findViewById(R.id.saveButton);
onSave.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
// Eventually I'll want to make this a for loop to perform this operation on all fragments created.
FragmentManager fragMan = getFragmentManager();
// this should get the fragment "1"
templateFragment1 fragTest = (templateFragment1) fragMan.findFragmentByTag("1");
// and this should run the method "editTextGetter()" inside of the previously specified fragment
String editTestText = fragTest.editTextGetter();
// just a quick way to show if I got the correct value, for debugging
Toast.makeText(getApplicationContext(), editTestText, Toast.LENGTH_SHORT).show();
}
});
}
}
activity_main.xml
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.liftrpoc.scheduler1.pushpulldetails"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/templateFragmentLayout"
android:orientation="vertical">
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/addDay"
android:text="Add Day"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/removeDay"
android:text="Remove Day"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/saveButton"
android:text="Save"
/>
</LinearLayout>
</ScrollView>
templateFragment1.java
public class templateFragment1 extends Fragment {
public templateFragment1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
LinearLayout view = (LinearLayout) inflater.inflate(R.layout.fragment_template_fragment1, container, false);
// a good bit of extra code that adds it's own instanced fragments, just like in the main activity.
// this shouldn't have anything to do with what we're trying to do though. It doesn't touch anything else.
return view;
}
// here's the editText method I'm trying to access from my main activity.
public String editTextGetter(){
EditText text = (EditText) getActivity().findViewById(R.id.editTextTest);
String value1 = text.getText().toString();
return value1;
}
}
fragment_template_fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E3E3E3"
android:orientation="vertical"
android:id="@+id/ParentLinearLayout1"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editTextTest"
/>
</LinearLay
最佳答案
使用 ArrayList 并将您制作的每个 fragment 存储在其中。然后,当您想调用 fragment 中的特定函数或获取 View 时,只需从 ArrayList 中获取所需的 fragment 即可完成您的工作。
关于java - 如何从父 Activity 以编程方式生成的 fragment 中调用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36685619/
我有一个层次结构中的项目列表,我正在尝试将此列表解析为实际的对象层次结构。我正在使用modified pre-order tree traversal存储/迭代此列表,所以我拥有的是树的子集,包括所有
给定这个类: public class Parent { public Child[] Children {get;set;} } 还有这个数组: Parent[] parents; 如何使用 L
我面临着一些难以完成这个看似简单的任务。 我在一个嵌套块 (Suggest.phtml) 中,我想从父块 (result.phtml) 获取一个值(搜索结果的数量,以及集合计数) 我错了还是这里没有简
我在访问子模型重复器字段以在没有用户操作的情况下加载时遇到问题,我在父模型 Controller 中使用 RelationController这就是我在父 Controller 中所做的 class
我有一个 datetime 指令,其范围内包含两个属性:date 和 time。如何从父 Controller 访问这些属性?我尝试这样做 Start: End: You
我的父类是: public Class Parent { protected void foo() { bar(); } protected void
所以我知道如何中断列表的继承 (SPList.BreakRoleInheritance(true)),但是如何以编程方式从其父级重新继承? 谢谢 最佳答案 MSDN link . SPList.Res
最近我们将maven版本更改为3.5.4根据https://issues.apache.org/jira/browse/MNG-5940 Maven Super POM 中的 maven-source
我需要在单击页面背景(#page)时触发一个事件(例如隐藏 float 购物车),但在单击内部内容时不会发生此事件。所以我需要这个事件发生在空间:页面减去内容。我该如何实现它?谢谢 如果我有这个结构:
我在另一个 ajax 函数中有一个 ajax 函数 $.ajax({ url: '../...', type: 'POST',
我创建了两个类 - Building 和 Warehouse。 Warehouse 扩展 Building。我创建了 Building map (building_map),其中放置了 Buildin
我有一个获取和处理数据的 Activity ,我想启动一个新 Activity ,该 Activity 能够从父 Activity 访问一些变量(复杂数据结构)。 我不能在这里使用序列化,有没有一种简
在 HTML 中,我有两个函数,一个是仅带有警报的 abc,另一个是使用 window.open() 创建一个新窗口,并填充预定义的 HTML 字符串。我想从父 HTML 调用该函数。我尝试过调用pa
我一直在研究这个脚本并且一切正常,除了 $(this).parents(".clonedInput").clone()。按钮克隆引用放置在 .clonedInput div 中。 如果能帮助我从 cl
我有以下代码: 主要 HTML 文档 $(function(){ $("#i").load(function(){ var $iframeConten
html 的一部分结构如下。我想从中获得工作“标题”和“时间”。我可以单独获取它们,例如: from bs4 import BeautifulSoup pages = ' \
我正在尝试从父 div 中删除底部边框。我已经编写了以下 HTML 代码,但我认为我的方法不正确。看到这张图片 这是我的 HTML 代码 j
通常,我可以从 Activity 调用扩展 DialogFragment 并实现 DialogInterface.OnClickListener() 的对象。 然后,如果我想从那个 Dialog 调用
我花了好几个小时搜索如何从嵌套 Controller 更新服务值。 我的子 Controller 需要更新服务中的值。该值需要显示在父 Controller 中。 我做了一个jsfiddle,让它更清
我有一个包含一些数据的表格,表格中的每个元素都是一个 React 类组件。它看起来像这样: 我想要的只是有一个用于“选中所有”功能的复选框(左上角的复选框)。问题是我不知道如何解决这个问题,因为 pr
我是一名优秀的程序员,十分优秀!