我试图在点击GridView
中的某个元素时调用某个活动
它将在滑动活动中打开,它将显示相应的图像,但我在设置时收到错误
如
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.union.project, PID: 2194
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.union.project/com.union.project.Swipeview.Main4Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
at com.union.project.Swipeview.Main4Activity.onCreate(Main4Activity.java:26)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
04-27 01:17:54.025 2194-2194/com.union.project I/Process: S
这是我点击anyitem时显示吐司然后崩溃我的片段代码的错误
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_two, container, false);
GridView gridView=(GridView)view.findViewById(R.id.gridView2);
gridView.setAdapter(new UAdapter(getActivity()));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(),"Pic"+(position)+"Selected",Toast.LENGTH_SHORT).show();
Intent intent= new Intent(view.getContext(),Main4Activity.class);
intent.putExtra("pic",position);
startActivity(intent);
}
});
return view;
}
我的滑动视图代码`
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
viewPager =(ViewPager)findViewById(R.id.viewpager1);
adapter =new CustomeSwipeAdapter2(this);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(getIntent().getIntExtra("pic", 0));
}`
这是我想要开始的活动但是得到错误可以告诉我如何解决这个问题。
答案 0 :(得分:4)
您的错误本身会回答您的问题。
requestFeature() must be called before adding content
这意味着您的第二个活动需要此更改
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main4);
requestWindowFeature()
必须在setContentView()
更新后的问题
现在你的错误是
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
您需要查看此What is a Null Pointer Exception, and how do I fix it?
答案 1 :(得分:1)
像{@ p>一样呼叫requestWindowFeature
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
}
答案 2 :(得分:0)
如上所述,您需要调用requestWindowFeature(Window.FEATURE_NO_TITLE);在setContentView方法之前。
答案 3 :(得分:0)
问题出在这些方面
viewPager =(ViewPager)findViewById(R.id.viewpager1);
adapter =new CustomeSwipeAdapter2(this);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(getIntent().getIntExtra("pic", 0));
首先:检查您的activity_main4 xml中是否有一个id为viewpager1的ViewPager布局
第二:如何将图像传递给适配器?是否有链接或你自己通过drawable?!你没有把任何东西传递给你的适配器!!!!!!!!!!!!!
第三:当您的适配器为空时,您无法使用
viewPager.setCurrentItem(getIntent().getIntExtra("pic", 0));
在它上面