Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:NullPointerException发生在ViewPager和PagerAdapter中_Java_Android_Android Studio - Fatal编程技术网

Java Android:NullPointerException发生在ViewPager和PagerAdapter中

Java Android:NullPointerException发生在ViewPager和PagerAdapter中,java,android,android-studio,Java,Android,Android Studio,我正在用NumberPickers写一个应用程序。如果没有使用数字选择器的任何方法,则数字选择器是正常的,但是当我调用NumberPicker.setMaxValue方法时,会发生NullPointer异常,并且应用程序无法启动。如果不使用set max ot set min方法,则不会发生错误。 更新:我认为这个错误可能是由于我使用了PagerAdapter和ViewPager造成的,因为问题中的数字导航器不在第一页。 以下是我的onCreate方法: protected void onCre

我正在用NumberPickers写一个应用程序。如果没有使用数字选择器的任何方法,则数字选择器是正常的,但是当我调用NumberPicker.setMaxValue方法时,会发生NullPointer异常,并且应用程序无法启动。如果不使用set max ot set min方法,则不会发生错误。 更新:我认为这个错误可能是由于我使用了PagerAdapter和ViewPager造成的,因为问题中的数字导航器不在第一页。 以下是我的onCreate方法:

protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 //All the pager stuff
 MyPagerAdapter adapter = new MyPagerAdapter();
 ViewPager myPager = (ViewPager) findViewById(R.id.pager);
 myPager.setAdapter(adapter);
 myPager.setCurrentItem(0);
 //Start of real code.
 final NumberPicker LGmade = (NumberPicker) findViewById(R.id.LGmade);
 //Code responsible for error follows
 LGmade.setMaxValue(3);
 LGmade.setMinValue(0);
 }
以下是XML:

<GridLayout 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=".MainActivity"
    android:background="@drawable/bc"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Low Goals"
        android:id="@+id/textView"
        android:layout_row="0"
        android:layout_column="0"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp" />

    <NumberPicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/LGmade"
        android:layout_row="3"
        android:layout_column="1"
        android:layout_marginTop="10dp"
        android:showDividers="middle"
        android:orientation="vertical"/>

    <NumberPicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/LGmissed"
        android:layout_row="3"
        android:layout_column="2"
        android:layout_marginTop="10dp"
        android:showDividers="middle"
        android:orientation="vertical"/>

</GridLayout>

根据所提供的信息,我想说

R.id.LGmade

可能不是正确的id,因此变量lgmake为null。

如果您试图在onCreate方法中初始化最终变量NumberPicker lgmake,这是不允许的。你可以简单地删除final关键字,或者在构造函数中初始化它

NOTE: 
A final variable can only be initialized once, either via an 
      initializer or an assignment statement. 
It does not need to be initialized at the point of declaration: this is called a 
      "blank final" variable. 
A blank final instance variable of a class must be definitely assigned in every 
      constructor of class in which it is declared
更改如下

final NumberPicker LGmade = (NumberPicker) findViewById(R.id.LGmade);


post activity_main layout xmlPlease,你能发布logcat吗?@user959631我添加了logcat你的错误在class MainActivity的第55行,你能告诉我们第55行是什么吗?第55行是lgmake.setMaxValue3;我添加了.xml文件。R.id似乎是正确的
final NumberPicker LGmade = (NumberPicker) findViewById(R.id.LGmade);
NumberPicker LGmade = (NumberPicker) findViewById(R.id.LGmade);