我想創建對話框的片段,但是當我調用setAdapter方法我有一個Java NullPointerException,並且我找不到該錯誤。Android的 - NullPointerException在ViewPager
Java代碼:
public class MainActivity extends FragmentActivity {
Context myContext;
private MyAdapter mAdapter;
private ViewPager mPager;
String TAG = "Dialog";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myContext = this;
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(myContext);
dialog.setContentView(R.layout.layout_dialog);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager) dialog.findViewById(R.id.myViewPager);
**mPager.setAdapter(mAdapter);** //Line 39, the error is on this line.
dialog.show();
}
});
}
public class MyAdapter extends FragmentPagerAdapter {
int counts;
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 3;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ImageFragment(R.drawable.icecream);
case 1:
return new ImageFragment(R.drawable.cupcake);
case 2:
return new ImageFragment(R.drawable.donut);
default:
return null;
}
}
}
}
片段類:
public class ImageFragment extends Fragment {
private final int imageResourceId;
public ImageFragment(int imageResourceId) {
this.imageResourceId = imageResourceId;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_dialog_item, container,
false);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView1);
imageView.setImageResource(imageResourceId);
return view;
}
}
activity_main XML代碼:
<RelativeLayout 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">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20dip"
android:layout_marginTop="30dip"
android:text="Show Dialog" />
</RelativeLayout>
layout_dialog.xml代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/myViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
activity_dialog_item.xml代碼:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/cupcake" />
</RelativeLayout>
錯誤:
03-06 13:58:22.546: E/AndroidRuntime(818): FATAL EXCEPTION: main
03-06 13:58:22.546: E/AndroidRuntime(818): java.lang.NullPointerException
03-06 13:58:22.546: E/AndroidRuntime(818): at com.example.viewpager.MainActivity$1.onClick(MainActivity.java:39)
03-06 13:58:22.546: E/AndroidRuntime(818): at android.view.View.performClick(View.java:2485)
03-06 13:58:22.546: E/AndroidRuntime(818): at android.view.View$PerformClick.run(View.java:9080)
03-06 13:58:22.546: E/AndroidRuntime(818): at android.os.Handler.handleCallback(Handler.java:587)
03-06 13:58:22.546: E/AndroidRuntime(818): at android.os.Handler.dispatchMessage(Handler.java:92)
03-06 13:58:22.546: E/AndroidRuntime(818): at android.os.Looper.loop(Looper.java:123)
03-06 13:58:22.546: E/AndroidRuntime(818): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-06 13:58:22.546: E/AndroidRuntime(818): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 13:58:22.546: E/AndroidRuntime(818): at java.lang.reflect.Method.invoke(Method.java:507)
03-06 13:58:22.546: E/AndroidRuntime(818): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-06 13:58:22.546: E/AndroidRuntime(818): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-06 13:58:22.546: E/AndroidRuntime(818): at dalvik.system.NativeStart.main(Native Method)
*您可能*指出MainActivity.java的第39行。然後,試着弄清楚那一行上的內容可能爲空 - 以及爲什麼。 =) – 2013-03-06 09:01:17
'MainActivity.java'上的第39行是什麼? – 2013-03-06 09:01:55
我已編輯我的問題並指出第39行 – Mansi 2013-03-06 09:03:27