从微调器中进行选择时,app force会关闭

时间:2013-12-06 07:01:37

标签: java android nullpointerexception

我在旋转器上遇到麻烦。它曾经奏效,然后上帝知道发生了什么。我认为问题出在微调器选择中的try-catch块中。我在模拟器中运行应用程序,就像我说的,它运行了3天,然后没有。包含代码和日志。谢谢你的帮助。

package com.example.sasquatchdos;

import java.io.*;

import android.os.Bundle;
import android.app.Activity;
import android.text.method.ScrollingMovementMethod;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;


public class MainActivity extends Activity implements OnItemSelectedListener {

    //Global variables
    private TextView venue_textForm2TextView;
    private EditText searchVenueEditText;
    private Spinner venueSpinner;
    private String selection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //add searchVenueEditText
        searchVenueEditText =  (EditText)findViewById(R.id.searchVenueEditTextForm1);

        //venueSpinner Setup
        //add spinner
        venueSpinner = (Spinner) findViewById(R.id.venueSpinner);

        //set venueSpinner to the OnSelectedListener
        venueSpinner.setOnItemSelectedListener(this);
        // Create an ArrayAdapter
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.venue_names, android.R.layout.simple_spinner_item);
        // Specify layout
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Add the adapter to the venueSpinner
        venueSpinner.setAdapter(adapter);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    public void searchButtonOnClick(View view){

            selection = searchVenueEditText.getText().toString();

            //searchVenue button if statement
            if (((selection.equals("Bourbon")) | (selection.equals("Bourbon Theater")))){
                    //change form to view_venue
                setContentView(R.layout.venue_text);

                //setup TextView on venue_text form
                venue_textForm2TextView = (TextView)findViewById(R.id.venue_textForm2TextView);

                //create inputStream
                InputStream input = getResources().openRawResource(R.raw.venue_bourbon);
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input));
                StringBuilder venueText = new StringBuilder();

                try {
                    String string;
                    while (( string = bufferedReader.readLine()) != null) {
                            venueText.append(string);
                            venueText.append('\n');
                            venueText.toString();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            venue_textForm2TextView.setMovementMethod( new ScrollingMovementMethod());
            venue_textForm2TextView.setText(venueText);


    }//end bourbon if statement

        if (((selection.equals("Shooters")) | (selection.equals("Sculley's Shooters")))){
            //change form to view_venue
            setContentView(R.layout.venue_text);

            //setup TextView on venue_text form
            venue_textForm2TextView = (TextView)findViewById(R.id.venue_textForm2TextView);

            //create inputStream
            InputStream input = getResources().openRawResource(R.raw.venue_shooters);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input));
            StringBuilder venueText = new StringBuilder();

            try {
                String string;
                while (( string = bufferedReader.readLine()) != null) {
                        venueText.append(string);
                        venueText.append('\n');
                        venueText.toString();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            venue_textForm2TextView.setMovementMethod( new ScrollingMovementMethod());
            venue_textForm2TextView.setText(venueText);
        }//end shooters if statement
}//end searchButton onClick

//resetButtonClick
public void resetButtonClick(View view){
    //clearSearch button
        searchVenueEditText.setText(R.string.search_bar_form_1);

}

//buttonClick for back button on venue_text form
public void backButtonClick(View view){
    setContentView(R.layout.main);
}


@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    //Consulted StackOverflow with problem of finding the position of the spinner
    //and setting the setContentView.  http://stackoverflow.com/questions/18091984/android-spinner-to-change-setcontentview

    TextView textView = (TextView)arg1;  //get the position of the spinner and set it to a textView
    String spinnerString = textView.getText().toString();  //set textView to a String to use for populating the form with the appropriate file from res/raw


    //spinner selection if statements
    if (spinnerString.equals("Bourbon Theater")){
        //change view
        setContentView(R.layout.venue_text);

        //create inputStream
        InputStream input = getResources().openRawResource(R.raw.venue_bourbon);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input));
        StringBuilder venueText = new StringBuilder();

            try {
                String string;
                while (( string = bufferedReader.readLine()) != null) {
                        venueText.append(string);
                        venueText.append("\n");
                        venueText.toString();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            venue_textForm2TextView.setMovementMethod( new ScrollingMovementMethod());
            venue_textForm2TextView.setText(venueText);

    }

    if (spinnerString.equals("Shooters")){
        //change view
        setContentView(R.layout.venue_text);

        //create inputStream
        InputStream input = getResources().openRawResource(R.raw.venue_shooters);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input));
        StringBuilder venueText = new StringBuilder();

            try {
                String string;
                while (( string = bufferedReader.readLine()) != null) {
                        venueText.append(string);
                        venueText.append("\n");
                        venueText.toString();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            venue_textForm2TextView.setMovementMethod( new ScrollingMovementMethod());
            venue_textForm2TextView.setText(venueText);
    // TODO Auto-generated method stub
    }
}

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

}
}

logcat窗口显示:

12-06 00:36:48.295: D/AndroidRuntime(334): Shutting down VM
12-06 00:36:48.325: W/dalvikvm(334): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-06 00:36:48.365: E/AndroidRuntime(334): FATAL EXCEPTION: main
12-06 00:36:48.365: E/AndroidRuntime(334): java.lang.NullPointerException
12-06 00:36:48.365: E/AndroidRuntime(334):  at  com.example.sasquatchdos.MainActivity.onItemSelected(MainActivity.java:186)
12-06 00:36:48.365: E/AndroidRuntime(334):  at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
12-06 00:36:48.365: E/AndroidRuntime(334):  at android.widget.AdapterView.access$200(AdapterView.java:42)
12-06 00:36:48.365: E/AndroidRuntime(334):  at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
12-06 00:36:48.365: E/AndroidRuntime(334):  at android.os.Handler.handleCallback(Handler.java:587)
12-06 00:36:48.365: E/AndroidRuntime(334):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-06 00:36:48.365: E/AndroidRuntime(334):  at android.os.Looper.loop(Looper.java:123)
12-06 00:36:48.365: E/AndroidRuntime(334):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-06 00:36:48.365: E/AndroidRuntime(334):  at java.lang.reflect.Method.invokeNative(Native Method)
12-06 00:36:48.365: E/AndroidRuntime(334):  at java.lang.reflect.Method.invoke(Method.java:507)
12-06 00:36:48.365: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-06 00:36:48.365: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-06 00:36:48.365: E/AndroidRuntime(334):  at dalvik.system.NativeStart.main(Native Method)
12-06 00:36:51.125: I/Process(334): Sending signal. PID: 334 SIG: 9

2 个答案:

答案 0 :(得分:0)

venue_textForm2TextView为空。它没有初始化。

if (((selection.equals("Bourbon")) | (selection.equals("Bourbon Theater")))){

应该更正为,

if (((selection.equals("Bourbon")) || (selection.equals("Bourbon Theater")))){

答案 1 :(得分:0)

在您的微调器的ItemClick中,您设置的是setContentView(R.layout.venue_text);,但您没有初始化venue_textForm2TextView

在ItemClick中添加以下行以初始化venue_textForm2TextView

venue_textForm2TextView = (TextView)findViewById(R.id.venue_textForm2TextView);