我NullPointerException
在调用片段的方法时得到了一个。
在我的MainActivity 中:
AddTab fragObj = (AddTab) getSupportFragmentManager().findFragmentById(R.layout.fragment_add);
fragObj.receiveURL(sharedText);
receiveURL() 是我的 Fragment AddTab 中的一个方法。
我pagerslidingtabstrip
在我的应用程序中使用来创建选项卡,这Fragment
是由它的PagerAdapter
类创建的。
我不确定创建 Tab 的新实例是否正确,因为已经存在一个。我也不知道如何访问该实例。
我现在得到一个NullPointerException
。谁能帮助我如何在片段中调用该方法?
编辑 :
AddTab.java:
public class AddTab extends Fragment implements View.OnClickListener {
...
}
片段添加.xml:
<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>