- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有带 viewpager 的应用程序。当我单击第一个 fragment 上的按钮时,我需要调用其他 fragment 方法。
这是我的邮件 Activity
view plainprint?
Note: Text content in the code blocks is automatically word-wrapped
package src.com.programmingmobile.pageviewer;
package src.com.programmingmobile.pageviewer;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.Toast;
public class PageViewActivity extends FragmentActivity implements Communicator{
MyPageAdapter pageAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_view);
List<Fragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
}catch(Exception e){
e.printStackTrace();
}
}
private List<Fragment> getFragments(){
List<Fragment> fList = new ArrayList<Fragment>();
fList.add(MyFragment.newInstance("Fragment 1"));
fList.add(MyFragment2.newInstance("Fragment 2"));
//fList.add(MyFragment.newInstance("Fragment 3"));
return fList;
}
private class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
@Override
public void respond(String data) {
// TODO Auto-generated method stub
MyFragment2 fragment = (MyFragment2) getSupportFragmentManager().findFragmentById(R.id.Fragment2);
fragment.print();
}
}
这是我的 fragment 1
view plainprint?
Note: Text content in the code blocks is automatically word-wrapped
package src.com.programmingmobile.pageviewer;
import java.io.InputStream;
import javax.net.ssl.ManagerFactoryParameters;
import com.epapyrus.plugpdf.SimpleDocumentReader;
import com.epapyrus.plugpdf.SimpleDocumentReaderListener;
import com.epapyrus.plugpdf.SimpleReaderFactory;
import com.epapyrus.plugpdf.core.viewer.DocumentState;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyFragment extends Fragment{
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
Communicator com;
public void onAttach(Activity a) {
super.onAttach(a);
com = (Communicator) a;
}
public static final MyFragment newInstance(String message) {
MyFragment f = new MyFragment();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String message = getArguments().getString(EXTRA_MESSAGE);
View v = inflater.inflate(R.layout.myfragment_layout, container, false);
TextView messageTextView = (TextView) v.findViewById(R.id.textView);
messageTextView.setText(message);
// OpenPdf();
LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(
R.layout.myfragment_layout, container, false);
// note that we're looking for a button with id="@+id/myButton" in your
// inflated layout
// Naturally, this can be any View; it doesn't have to be a button
Button mButton = (Button) mLinearLayout.findViewById(R.id.button1);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
com.respond("Fragment ");
}
});
return mLinearLayout;
}
}
这是 fragment 2
view plainprint?
Note: Text content in the code blocks is automatically word-wrapped
package src.com.programmingmobile.pageviewer;
import java.io.InputStream;
import com.epapyrus.plugpdf.SimpleDocumentReader;
import com.epapyrus.plugpdf.SimpleDocumentReaderListener;
import com.epapyrus.plugpdf.SimpleReaderFactory;
import com.epapyrus.plugpdf.core.viewer.DocumentState;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MyFragment2 extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
// create a listener for receiving provide pdf loading results
SimpleDocumentReaderListener m_listener = new SimpleDocumentReaderListener() {
@Override
public void onLoadFinish(DocumentState.OPEN state) {
}
};
public static final MyFragment2 newInstance(String message) {
MyFragment2 f = new MyFragment2();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String message = getArguments().getString(EXTRA_MESSAGE);
View v = inflater.inflate(R.layout.myfragment_layout2, container, false);
TextView messageTextView = (TextView) v.findViewById(R.id.textView);
messageTextView.setText(message);
//OpenPdf();
return v;
}
public void print(){
Toast.makeText(getActivity(), " inside Respond ", 100);
}
}
这是 myfragment_layout xml 文件
view plainprint?
Note: Text content in the code blocks is automatically word-wrapped
package src.com.programmingmobile.pageviewer;
import java.io.InputStream;
import javax.net.ssl.ManagerFactoryParameters;
import com.epapyrus.plugpdf.SimpleDocumentReader;
import com.epapyrus.plugpdf.SimpleDocumentReaderListener;
import com.epapyrus.plugpdf.SimpleReaderFactory;
import com.epapyrus.plugpdf.core.viewer.DocumentState;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyFragment extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
Communicator com;
public void onAttach(Activity a) {
super.onAttach(a);
com = (Communicator) com;
}
public static final MyFragment newInstance(String message) {
MyFragment f = new MyFragment();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String message = getArguments().getString(EXTRA_MESSAGE);
View v = inflater.inflate(R.layout.myfragment_layout, container, false);
TextView messageTextView = (TextView) v.findViewById(R.id.textView);
messageTextView.setText(message);
// OpenPdf();
return v;
}
public void onclick(View v) {
com.respond("Fragment ");
}
}
这是 my_fragment2 xml 文件
view plainprint?
Note: Text content in the code blocks is automatically word-wrapped
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/Fragment2"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</RelativeLayout>
当我运行该应用程序时,它会给我第一个带有按钮的 fragment 。当我点击按钮时,它会给我以下错误
view plainprint?
Note: Text content in the code blocks is automatically word-wrapped
10-07 09:37:52.610: E/AndroidRuntime(10907): FATAL EXCEPTION: main
10-07 09:37:52.610: E/AndroidRuntime(10907): java.lang.NullPointerException
10-07 09:37:52.610: E/AndroidRuntime(10907): at src.com.programmingmobile.pageviewer.PageViewActivity.respond(PageViewActivity.java:66)
10-07 09:37:52.610: E/AndroidRuntime(10907): at src.com.programmingmobile.pageviewer.MyFragment$1.onClick(MyFragment.java:63)
10-07 09:37:52.610: E/AndroidRuntime(10907): at android.view.View.performClick(View.java:3620)
10-07 09:37:52.610: E/AndroidRuntime(10907): at android.view.View$PerformClick.run(View.java:14322)
10-07 09:37:52.610: E/AndroidRuntime(10907): at android.os.Handler.handleCallback(Handler.java:605)
10-07 09:37:52.610: E/AndroidRuntime(10907): at android.os.Handler.dispatchMessage(Handler.java:92)
10-07 09:37:52.610: E/AndroidRuntime(10907): at android.os.Looper.loop(Looper.java:137)
10-07 09:37:52.610: E/AndroidRuntime(10907): at android.app.ActivityThread.main(ActivityThread.java:4507)
10-07 09:37:52.610: E/AndroidRuntime(10907): at java.lang.reflect.Method.invokeNative(Native Method)
10-07 09:37:52.610: E/AndroidRuntime(10907): at java.lang.reflect.Method.invoke(Method.java:511)
10-07 09:37:52.610: E/AndroidRuntime(10907): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
10-07 09:37:52.610: E/AndroidRuntime(10907): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
10-07 09:37:52.610: E/AndroidRuntime(10907): at dalvik.system.NativeStart.main(Native Method)
给出空指针的行在下面
view plainprint?
Note: Text content in the code blocks is automatically word-wrapped
MyFragment2 fragment = (MyFragment2) getSupportFragmentManager().findFragmentById(R.id.Fragment2);
最佳答案
查看 source for FragmentPagerAdapter , 他们使用 FragmentManager.findFragmentByTag(name)
其中 name
的形式是
"android:switcher:" + viewId + ":" + id;
其中 viewId
是 ViewPager
的 android:id
而 id
是 的结果getItemId(position)
(在您的情况下可能只是位置)。
注意:这是不安全的,并且可能会随着支持库的版本而变化。它在所有以前的版本中都保持不变,但您应该考虑 setting up an event callback interface在您的两个组件之间(从技术上讲,在您的第一个 fragment 、 Activity 和第二个 fragment 之间)。
关于android - getSupportFragmentManager().findFragmentById 给了我一个 nullPointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26227461/
当在类的开头使用上下文和资源初始化 MediaPlayer 对象时,它会抛出 NullPointer 异常,但是当在类的开头声明它时(因此它是 null),然后以相同的方式初始化它在onCreate方
在这个问题中NullPointer when call entity Manager建议将 @Autowire DAO 添加到 Controller 中。我做到了并且工作正常。然后我在 DaoImpl
我被分配了一个项目,我必须在java中创建一个登录系统,使用filein将帐户写入数组,并通过确认输入用户名与中的用户名相同来使用该数组进行登录数组。我的用户名和密码位于正在读取的 .txt 文件中的
我得到了以下代码: - (void) hide_waiting_activity { [UIView beginAnimations:nil context:NULL]; {
在为学校开发一个程序时,我创建了一个方法,该方法根据您为该方法提供的值返回数组的索引。该方法抛出 NullPointerException 但数组的每个索引都已被实例化。为了说明这一点,我创建了一个执
在我的 JSP 页面上,我需要显示一个列表,其中包含/public 文件夹中当前存在的所有文件。 这是一个带有 spring 和 hibernate 的 j2ee 项目。在我的用于获取包含所有文件的列
当我更新大量 JavaFX 图表时,我收到以下 NullpointerException: java.lang.NullPointerException at com.sun.scenario
我是 android 编程的新手,但不是 Java。 我在以下位置收到有关 NPE 的错误消息: 12-09 10:29:58.247: D/dalvikvm(471): GC freed 711 o
我有两个 java 类。 Schedule 是使用名为 deadline 的作业数组的主要类。我在将任何东西放入数组时遇到问题。我有一个 for 循环,它从文本文件中读取数据(它工作正常)并将某些数据
我正在尝试从我的项目目录访问一个文件,我们称它为 src/main/project/foo/bar.txt 在类MyClass 但是当我这样做的时候: InputStream is = MyClass
您好,在我的应用程序中实现搜索小部件时遇到问题。它似乎无法找到菜单项的“actionview”,但它可以很好地找到该项目。 我四处寻找答案,但没有看到明确的解决方案。 这是我用 XML 声明的菜单
我不确定为什么会收到此错误,它以前是有效的,我们将不胜感激。尝试在微调器选择时产生一个新 Activity (我有一个内部有 ListView 的工作版本,我决定分支尝试微调器,走了很远然后迷路了试图
我得到一个 NullPointerException在我第一次使用 createEntityManager() 连接到数据库时,在我的 tomcat 控制台上.我正在使用: apache tomcat
我目前正在处理一个项目,该项目使用 JFrame 在 GUI 上运行。 我想尽可能清楚我的问题是什么,我正在使用 3 个包(实体、控制(管理器)、边界(GUI))。 边界进口控制,控制进口实体。 在
我正在将S3的 Parquet 文件加载到在EMR机器中运行的Hive数据仓库中: Release: emr-5.1.0Hadoop distribution: Amazon 2.7.3Applica
我有一个如下所示的方法: try { doStuff(); } catch (Exception ex) { logger.error(ex); } (我并没有真正使用像 doStuff 这样
谁能告诉我这段代码有什么问题 我得到空指针异常,我假设是因为多个请求 @Service public class GernericMockingServiceImpl implements Gener
首先感谢您指出我在创建这篇文章时所犯的错误。 我尽可能缩小了代码范围,希望现在更清楚了。 因此,发生的问题是 NullPointer Exception 例如,当我尝试从一个场景(根)切换到另一个场景
我已经遇到这个非常烦人的问题几天了,而且我似乎无法完全解决它。我正在尝试打开 .csv 文件,因此我将其导入到项目的 res/raw/ 文件夹中。然后我尝试通过 getResources() 方法打开
我想做一个简单的java应用程序来播放视频。我特别希望它能够播放 mpeg4 和 mov 格式。 JMF 是我开始的地方,我有一个很好的工作示例。但是,不支持 mov 或 mpeg4 格式。我看过 X
我是一名优秀的程序员,十分优秀!