[英]Android Fragment .setAdapter giving null pointer Exception onClick
I have the following Android Class: 我有以下Android类:
package com.gloxci.studentreport;
import java.util.ArrayList;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Spinner;
public class ExamScheduleFragment extends CustomFragment{
public ExamScheduleFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewPagerAdapter pAdapter=new ViewPagerAdapter(getActivity(), new RelativeLayout[]{setLayout1(inflater)});
vp=new CustomViewPager(getActivity());
vp.setAdapter(pAdapter);
vp.setPagingEnabled(false);
vp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
vp.setCurrentItem(1);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
return vp;
}
RelativeLayout setLayout1(LayoutInflater inflater)
{
final RelativeLayout rlayout2=(RelativeLayout)inflater.inflate(R.layout.attendance_stdlist, null);
Button btnSave=(Button)rlayout2.findViewById(R.id.btnSave);
btnSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stu
ArrayList<String> subjects=new ArrayList<String>();
subjects.add("Hello");
subjects.add("Option1");
subjects.add("Option2");
Spinner ExamSpinner = (Spinner)rlayout2.findViewById(R.id.SpinnerExam);
ArrayAdapter <String> subjectAdapter = new ArrayAdapter(getActivity(),
android.R.layout.simple_spinner_item, subjects);
subjectAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ExamSpinner.setAdapter(subjectAdapter);
}
});
return rlayout2;
}
}
and the following XML: 以及以下XML:
<?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:orientation="vertical"
>
<Button
android:id="@+id/btnSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Save"
/>
<Spinner
android:id="@+id/SpinnerExam"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Exam Title"
/>
</RelativeLayout>
and CustomFragment:
和
CustomFragment:
package com.gloxci.studentreport;
import android.app.Fragment;
public class CustomFragment extends Fragment{
CustomViewPager vp;
int SelectedIndex=-1;
String lastTitle="";
int onBack()
{
int retVal=vp.getCurrentItem();
vp.previous();
if(lastTitle.length()>0)
MainActivity.setTitle(lastTitle);
return retVal;
}
}
The problem is that the line ExamSpinner.setAdapter(subjectAdapter);
问题是该行
ExamSpinner.setAdapter(subjectAdapter);
is causing a nullpointerException
and crashing the app, when btnSave
is clicked. 单击
btnSave
时,会导致nullpointerException
并使应用程序崩溃。
As far as I can see everything is present that needs to be and it is all set up correctly. 据我所知,所有存在的东西都已经存在,并且都已正确设置。 The exact same setup is working fine in another class.
完全相同的设置在另一个类中也可以正常工作。
This is a project created by someone else that I am working on. 这是我正在研究的其他人创建的项目。 I am new to fragements.
我是零碎的新手。
Here is the LogCat: 这是LogCat:
12-29 23:21:22.078: E/AndroidRuntime(4165): FATAL EXCEPTION: main
12-29 23:21:22.078: E/AndroidRuntime(4165): java.lang.NullPointerException
12-29 23:21:22.078: E/AndroidRuntime(4165): at com.gloxci.studentreport.ExamScheduleFragment$2.onClick(ExamScheduleFragment.java:85)
12-29 23:21:22.078: E/AndroidRuntime(4165): at android.view.View.performClick(View.java:4084)
12-29 23:21:22.078: E/AndroidRuntime(4165): at android.view.View$PerformClick.run(View.java:16966)
12-29 23:21:22.078: E/AndroidRuntime(4165): at android.os.Handler.handleCallback(Handler.java:615)
12-29 23:21:22.078: E/AndroidRuntime(4165): at android.os.Handler.dispatchMessage(Handler.java:92)
12-29 23:21:22.078: E/AndroidRuntime(4165): at android.os.Looper.loop(Looper.java:137)
12-29 23:21:22.078: E/AndroidRuntime(4165): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-29 23:21:22.078: E/AndroidRuntime(4165): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 23:21:22.078: E/AndroidRuntime(4165): at java.lang.reflect.Method.invoke(Method.java:511)
12-29 23:21:22.078: E/AndroidRuntime(4165): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-29 23:21:22.078: E/AndroidRuntime(4165): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-29 23:21:22.078: E/AndroidRuntime(4165): at dalvik.system.NativeStart.main(Native Method)
12-29 23:24:09.020: E/Trace(10410): error opening trace file: No such file or directory (2)
here inflated layout is defferent
from the xml layout, your are creating ViewPager instead of inflating the the xml layout
. 这里
inflated layout is defferent
不同于xml布局,您正在creating ViewPager instead of inflating the the xml layout
。
write like this: in your onCreateView
. 这样写:在您的
onCreateView
。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.your_layout_name, null);
return inflate;
}
For anyone who is interested to know, the problem was that with the setup I have, I needed to use a listview to setAdapter. 对于任何有兴趣了解的人,问题在于我的安装程序中,我需要使用列表视图来设置适配器。 The code that worked is as follows:
起作用的代码如下:
final RelativeLayout rlayout2=(RelativeLayout)inflater.inflate(R.layout.attendance_stdlist, null);
ListView listview=(ListView)rlayout2.findViewById(R.id.list1);
and then to set the Adapter I used: 然后设置我使用的适配器:
Spinner ExamSpinner = (Spinner)rlayout2.findViewById(R.id.SpinnerExam);
ListView listview=(ListView)rlayout2.findViewById(R.id.list1);
ArrayAdapter <String> subjectAdapter = new ArrayAdapter(getActivity(),
android.R.layout.simple_spinner_item, subjects);
subjectAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
listview.setAdapter(subjectAdapter);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.