Java 从活动调用片段方法时出现NullPointerException?
调用片段的方法时,我得到一个Java 从活动调用片段方法时出现NullPointerException?,java,android,android-fragments,nullpointerexception,pagerslidingtabstrip,Java,Android,Android Fragments,Nullpointerexception,Pagerslidingtabstrip,调用片段的方法时,我得到一个NullPointerException 在我的main活动中: AddTab fragObj = (AddTab) getSupportFragmentManager().findFragmentById(R.layout.fragment_add); fragObj.receiveURL(sharedText); public class AddTab extends Fragment implements View.OnClickListener {
NullPointerException
在我的main活动中:
AddTab fragObj = (AddTab) getSupportFragmentManager().findFragmentById(R.layout.fragment_add);
fragObj.receiveURL(sharedText);
public class AddTab extends Fragment implements View.OnClickListener {
<FrameLayout 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" tools:context="com.example.nikhil.amazon1.Add">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:text="@string/URL"
android:hint="@string/hint"
android:layout_marginTop="200dp"
android:layout_gravity="center_horizontal|top" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Track"
android:id="@+id/button"
android:layout_gravity="center" />
</FrameLayout>
receiveURL()是my Fragment AddTab中的一个方法
我在我的应用程序中使用pagerslidingAbstrip
来创建选项卡,Fragment
是由它的PagerAdapter
类创建的
我不确定创建选项卡的新实例是否正确,因为已经存在一个实例。我也不知道如何访问该实例
我现在得到一个NullPointerException
。有人能帮我在片段中如何调用该方法吗
编辑:
AddTab fragObj = (AddTab) getSupportFragmentManager().findFragmentById(R.layout.fragment_add);
fragObj.receiveURL(sharedText);
public class AddTab extends Fragment implements View.OnClickListener {
<FrameLayout 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" tools:context="com.example.nikhil.amazon1.Add">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:text="@string/URL"
android:hint="@string/hint"
android:layout_marginTop="200dp"
android:layout_gravity="center_horizontal|top" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Track"
android:id="@+id/button"
android:layout_gravity="center" />
</FrameLayout>
AddTab.java:
AddTab fragObj = (AddTab) getSupportFragmentManager().findFragmentById(R.layout.fragment_add);
fragObj.receiveURL(sharedText);
public class AddTab extends Fragment implements View.OnClickListener {
<FrameLayout 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" tools:context="com.example.nikhil.amazon1.Add">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:text="@string/URL"
android:hint="@string/hint"
android:layout_marginTop="200dp"
android:layout_gravity="center_horizontal|top" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Track"
android:id="@+id/button"
android:layout_gravity="center" />
</FrameLayout>
}
fragment\u add.xml:
AddTab fragObj = (AddTab) getSupportFragmentManager().findFragmentById(R.layout.fragment_add);
fragObj.receiveURL(sharedText);
public class AddTab extends Fragment implements View.OnClickListener {
<FrameLayout 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" tools:context="com.example.nikhil.amazon1.Add">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:text="@string/URL"
android:hint="@string/hint"
android:layout_marginTop="200dp"
android:layout_gravity="center_horizontal|top" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Track"
android:id="@+id/button"
android:layout_gravity="center" />
</FrameLayout>
如果使用
标记在布局中包含片段
,则可以使用为xml中的片段
指定的id使用findfindFragmentById
。例如:
<fragment
...
android:id="@+id/fragment_id/>
如果动态添加片段
,请使用标记:
String TAG = "fragment tag";
FragmentTransaction ft;
ft.add(containerViewId, fragment, TAG);
及
我怀疑你的片段id真的是R.layout.fragment\u add。。。您是否使用add方法动态添加片段?哎呀!这是我片段的布局,我什么也没找到,所以我认为它是id。我错了吗?看看我的答案是否对你有帮助。我不太确定将findFragmentById或findFragmentByTag与ViewPager结合使用会产生什么结果。如果你决定试一试,请让我知道它是否适合你,我很想知道。。。如果它确实返回了一些东西,我建议在触摸它的UI之前检查片段是否可见。谢谢您的输入@2Dee。但是,正如您在问题中所看到的那样,我的XML中似乎没有。我有一个例子,framelayout在您的片段布局中,片段标记通常包含在活动布局中,因此我指的是在活动的XML中添加的片段(主要是为了帮助其他人查看此问题)。注意,我还提供了一种方法,使用FragmentTransaction动态添加片段。