简体   繁体   中英

Using Spinners and have force close error - ANDROID

---- I have tried tomove the spinner and textview variables under setcontentview but then spinners are not known publicly so i could use them in void onitemselected what should i do ??? ------

----- and also I am not sure if I have used spinners correctly -----

Ok So I want to use spinners to get my values from them and compare it with the randomly generated ones and if they are correct shows them in textview as green if not as red and I only did this IF statement for one spinner just to test it but it force closes.

MainPage.java

package com.example.decrypter;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class MainPage extends Activity implements OnItemSelectedListener {
    double random1;
    int check1,check2,check3,check4,check5;

    EditText textbox;
    int guess;
    String s;
    Spinner spinner1 = (Spinner) findViewById(R.id.spinner01);
    Spinner spinner2 = (Spinner) findViewById(R.id.spinner02);
    Spinner spinner3 = (Spinner) findViewById(R.id.spinner03);
    Spinner spinner4 = (Spinner) findViewById(R.id.spinner04);
    Spinner spinner5 = (Spinner) findViewById(R.id.spinner05);
    TextView display1 = (TextView) findViewById(R.id.txtdisplay1);
    TextView display2 = (TextView) findViewById(R.id.txtdisplay2);
    TextView display3 = (TextView) findViewById(R.id.txtdisplay3);
    TextView display4 = (TextView) findViewById(R.id.txtdisplay4);
    TextView display5 = (TextView) findViewById(R.id.txtdisplay5);

    Integer[] numbers = {1,2,3,4,5,6,7,8,9};

    Button btnrandom = (Button) findViewById(R.id.btnrandom);
    Button btn1 = (Button) findViewById(R.id.btn1);
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        ArrayAdapter <Integer> adapter1 = new ArrayAdapter <Integer>(this,       android.R.layout.simple_spinner_item, numbers);

        setContentView(R.layout.activity_main_page);
        spinner1.setAdapter(adapter1);
        spinner1.setOnItemSelectedListener(this);
        spinner2.setAdapter(adapter1);
        spinner2.setOnItemSelectedListener(this);
        spinner3.setAdapter(adapter1);
        spinner3.setOnItemSelectedListener(this);
        spinner4.setAdapter(adapter1);
        spinner4.setOnItemSelectedListener(this);
        spinner5.setAdapter(adapter1);
        spinner5.setOnItemSelectedListener(this);

        btnrandom.setOnClickListener( new View.OnClickListener() 
        { 

           public void onClick(View v) {
            // TODO Auto-generated method stub
            random1 = Math.floor(Math.random()*10); 
            //display.setText("random:" + random1);     
            /*check1 = Integer.parseInt(spinner1.getSelectedItem().toString())     ; 
            */
        }
       });

        btn1.setOnClickListener( new View.OnClickListener() 
        { 

           public void onClick(View v) {
            // TODO Auto-generated method stub

            if(check1==random1){
                display1.setTextColor(Color.GREEN);
                display1.setText(s);
            }
            else{
                display1.setTextColor(Color.RED);
                display1.setText(s);
            }   
        }
       });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main_page, menu);
        return true;

    }

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub

        check1 = Integer.parseInt(spinner1.getSelectedItem().toString());
        check2 = Integer.parseInt(spinner2.getSelectedItem().toString()) ;
        check3 = Integer.parseInt(spinner3.getSelectedItem().toString()) ;
        check4 = Integer.parseInt(spinner4.getSelectedItem().toString()) ;
        check5 = Integer.parseInt(spinner5.getSelectedItem().toString()) ;

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
}

AndroidManifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.decrypter"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainPage"
        android:label="@string/title_activity_main_page" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

public class MainPage extends Activity implements OnItemSelectedListener {
    double random1;
    int check1,check2,check3,check4,check5;

    EditText textbox;
    int guess;
    String s;
Spinner spinner1,spinner2,spinner3,spinner4,spinner5;
  TextView display1,display2,display3,display4,display5;
Button btnrandom,btn1;

    Integer[] numbers = {1,2,3,4,5,6,7,8,9};


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

              setContentView(R.layout.activity_main_page);
 spinner1 = (Spinner)findViewById(R.id.spinner01);
     spinner2 = (Spinner)findViewById(R.id.spinner02);
     spinner3 = (Spinner)findViewById(R.id.spinner03);
     spinner4 = (Spinner)findViewById(R.id.spinner04);
     spinner5 = (Spinner)findViewById(R.id.spinner05);
     display1 = (TextView)findViewById(R.id.txtdisplay1);
     display2 = (TextView)findViewById(R.id.txtdisplay2);
     display3 = (TextView)findViewById(R.id.txtdisplay3);
     display4 = (TextView)findViewById(R.id.txtdisplay4);
    display5 = (TextView)findViewById(R.id.txtdisplay5);
 btnrandom = (Button)findViewById(R.id.btnrandom);
    btn1 = (Button)findViewById(R.id.btn1);

        spinner1.setAdapter(adapter1);
        spinner1.setOnItemSelectedListener(this);
        spinner2.setAdapter(adapter1);
        spinner2.setOnItemSelectedListener(this);
        spinner3.setAdapter(adapter1);
        spinner3.setOnItemSelectedListener(this);
        spinner4.setAdapter(adapter1);
        spinner4.setOnItemSelectedListener(this);
        spinner5.setAdapter(adapter1);
        spinner5.setOnItemSelectedListener(this);

  ArrayAdapter <Integer> adapter1 = new ArrayAdapter <Integer>(this,       android.R.layout.simple_spinner_item, numbers);

        btnrandom.setOnClickListener( new View.OnClickListener() 
        { 

           public void onClick(View v) {
            // TODO Auto-generated method stub
            random1 = Math.floor(Math.random()*10); 
            //display.setText("random:" + random1);     
            /*check1 = Integer.parseInt(spinner1.getSelectedItem().toString())     ; 
            */
        }
       });

        btn1.setOnClickListener( new View.OnClickListener() 
        { 

           public void onClick(View v) {
            // TODO Auto-generated method stub

            if(check1==random1){
                display1.setTextColor(Color.GREEN);
                display1.setText(s);
            }
            else{
                display1.setTextColor(Color.RED);
                display1.setText(s);
            }   
        }
       });
    }

The force close is very likely being caused as a result of a null pointer exception on display1. You should look for the instance in your onCreate() after you have initialized your layout else android is not going to be able to find that spinner via the findViewById()

Refer to: http://developer.android.com/training/basics/activity-lifecycle/starting.html#Create

You are doing this Spinner spinner1 = (Spinner) findViewById(R.id.spinner01);

in your class when you declare the variables. These declarations are outside any method in your class and are therefore evaluated when your class is loaded and before your method code (beginning with onCreate() is called.

findViewById searches the currently loaded layout for a view with the matching id and returns the instance of it if found. If not found, or no layout is loaded, it will return null.

Because it returns null, the first time you try to use the instance, you get a null pointer exception.

In your case, you must use fndViewById after you've called setContentView() which you should do in onCreate().

To fix your problem, declare the variable at the class level like this Spinner spinner1; then after setContentView, set the instance like this spinner1 = (Spinner) findViewById(R.id.spinner01);

It's perfectly logical for findViewById to return null rather than cause an exception if a view doesn't exist because in some scenarios, complex dynamic layouts might be determined at runtime according to some user input so something like if (findViewById(R.id.someview)==null) is valid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM