【问题标题】:Nullpointer setadapter in spinner微调器中的 Nullpointer setadapter
【发布时间】:2014-09-04 01:17:40
【问题描述】:

这是我的代码有错误。 我认为一切都很好,但我不知道错误在哪里 这是我的代码:

public class MainActivity extends Activity implements OnItemSelectedListener{
private EditText promAcum;
private EditText credCur;
private EditText sem;
private EditText num_examenes;
private EditText editText13;
private EditText editText14;
private EditText editText15;
private EditText editText16;
private EditText editText17;
private EditText editText18;
private EditText editText19;
private EditText editText20;
private EditText editText21;
private Spinner spinner1;
public String valorPromAcum;
public String valorCredCur;
public String valorSem;
public String[] numeroExamenes={"3","4","5","6"};
private final static String TAG_FRAGMENT = "TAG_FRAGMENT";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FragmentManager fm = getFragmentManager();
    Fragment fragment = fm.findFragmentById(R.id.fragment_content);

    if (fragment == null) {
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.fragment_content, new MainFragment());
        ft.commit();
    }

    promAcum = (EditText) findViewById(R.id.promAcum);
    credCur = (EditText) findViewById(R.id.credCur);
    sem = (EditText) findViewById(R.id.sem);
    editText13 = (EditText)findViewById(R.id.editText13);
    editText14 = (EditText)findViewById(R.id.editText14);
    editText15 = (EditText)findViewById(R.id.editText15);
    editText16 = (EditText)findViewById(R.id.editText16);
    editText17 = (EditText)findViewById(R.id.editText17);
    editText18 = (EditText)findViewById(R.id.editText18);
    editText19 = (EditText)findViewById(R.id.editText19);
    editText20 = (EditText)findViewById(R.id.editText20);
    editText21 = (EditText)findViewById(R.id.editText21);

    spinner1 = (Spinner) findViewById(R.id.spinner1);

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>    (this,android.R.layout.simple_spinner_item,numeroExamenes);

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(dataAdapter);

    addListenerOnSpinnerItemSelection();
}

public void addListenerOnSpinnerItemSelection(){

    spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view,

                int position, long id) {
               Toast.makeText(MainActivity.this,
                        "On Button Click : " + 
                        "\n" + String.valueOf(spinner1.getSelectedItem()) ,
                        Toast.LENGTH_LONG).show();
            }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub
        }
    });}

这是我的 xml 代码

<LinearLayout 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"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="@string/materias"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:ems="10" 
        android:inputType="text" >

    </EditText>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3" >
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/creditos"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="number" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3" 
    android:layout_marginBottom="30sp">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cantidad de Examenes"
        android:textSize="20sp" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="fill_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:textSize="20sp"
                android:text="Examen" />
            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:textSize="20sp"
                android:text="Nota" />
            <TextView
                android:id="@+id/textView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:gravity="center"
                android:layout_weight="2"
                android:text="%" />

        </TableRow>
</LinearLayout>  

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="fill_parent"
            android:layout_height="match_parent">

            <EditText
                android:id="@+id/editText4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10" 
                android:inputType="text"/>

            <EditText
                android:id="@+id/editText5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:layout_weight="1"
                android:ems="10"
                android:inputType="numberDecimal"/>

            <EditText
                android:id="@+id/editText6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10" 
                android:inputType="number"/>

        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="fill_parent"
            android:layout_height="match_parent">

            <EditText
                android:id="@+id/editText7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10" 
                android:inputType="text"/>

            <EditText
                android:id="@+id/editText8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:layout_weight="1"
                android:ems="10"
                android:inputType="numberDecimal"/>

            <EditText
                android:id="@+id/editText9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10" 
                android:inputType="number"/>

        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="fill_parent"
            android:layout_height="match_parent">

            <EditText
                android:id="@+id/editText10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10" 
                android:inputType="text"/>

            <EditText
                android:id="@+id/editText11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:layout_weight="1"
                android:ems="10"
                android:inputType="numberDecimal"/>

            <EditText
                android:id="@+id/editText12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10" 
                android:inputType="number"/>

        </TableRow>

        <TableRow
            android:id="@+id/tableRow5"
            android:layout_width="fill_parent"
            android:layout_height="match_parent">

            <EditText
                android:id="@+id/editText13"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10" 
                android:inputType="text"/>

            <EditText
                android:id="@+id/editText14"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:layout_weight="1"
                android:ems="10"
                android:inputType="numberDecimal"/>

            <EditText
                android:id="@+id/editText15"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10" 
                android:inputType="number"/>

        </TableRow>

        <TableRow
            android:id="@+id/tableRow6"
            android:layout_width="fill_parent"
            android:layout_height="match_parent">

            <EditText
                android:id="@+id/editText16"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10" 
                android:inputType="text"/>

            <EditText
                android:id="@+id/editText17"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:layout_weight="1"
                android:ems="10"
                android:inputType="numberDecimal"/>

            <EditText
                android:id="@+id/editText18"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10" 
                android:inputType="number"/>

        </TableRow>

        <TableRow
            android:id="@+id/tableRow7"
            android:layout_width="fill_parent"
            android:layout_height="match_parent">

            <EditText
                android:id="@+id/editText19"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10" 
                android:inputType="text"/>

            <EditText
                android:id="@+id/editText20"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:layout_weight="1"
                android:ems="10"
                android:inputType="numberDecimal"/>

            <EditText
                android:id="@+id/editText21"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="number" />

        </TableRow>

 </LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/guardar" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:onClick="agregarNotas"
        android:text="@string/materias" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="@string/nueva" />
</LinearLayout>

logcat 错误第 78 行:spinner1.setadapter(adapter);

enter code here09-03 11:25:46.344: E/AndroidRuntime(7841): Caused by: java.lang.NullPointerException
09-03 11:25:46.344: E/AndroidRuntime(7841): at com.jonathanar.promapp.MainActivity.onCreate(MainActivity.java:78)

欢迎提出任何建议

【问题讨论】:

  • 是帖子 xml 名称 activity_main。如果不是,视图可能属于片段布局。

标签: java android eclipse nullpointerexception spinner


【解决方案1】:

您正在尝试从Activity 检索视图,但它们位于Fragment

getActivity().findViewById(R.id.yourId); 是您在使用findViewById() 方法时应该执行的操作。

【讨论】:

    【解决方案2】:

    NullPointerException 不是实例化适配器的问题,而是与获取 Spinner 的方式有关。

    如果片段中存在微调器,则需要指定其上下文。

    只需将 findViewById() 放入 this.findViewById(),这是您的活动。换句话说,您告诉方法在活动的内容视图中查找视图。

    如果微调器在片段的内容视图中,则需要改为使用

    fragment.getActivity().findViewById(R.id.???);
    

    这样 findViewById() 知道在片段的上下文中找到你的微调器。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2015-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2012-04-12
      相关资源
      最近更新 更多