单击按钮无法转到其他活动

时间:2014-04-15 09:58:07

标签: android android-spinner

我正在编写一个Android代码,如果用户选择一个特定的单选按钮,然后单击另一个按钮,则应打开一个新页面。另外,将一个微调器的值传递给另一个页面。当我执行我的代码时,应用程序崩溃。代码如下:

public class LeaveApp extends Activity {
 private TextView Output1,Output2;
 EditText e1;
    private Button changeDate1,changeDate2,btn1;
    int flag1=0,flag2=0;
    private int year;
    private int month;
    private int day;
    private Spinner spinner2;
    static final int DATE_PICKER_ID1 = 1111; 
    static final int DATE_PICKER_ID2 = 1110;
   // RadioButton rb1,rb2;
    RadioGroup rg2;
    String locationSet = null;
    String strLocation = null;
    Location arrLocation;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.leaveapp);

        Output1 = (TextView) findViewById(R.id.textView7);
        Output2= (TextView) findViewById(R.id.textView8);
        changeDate1 = (Button) findViewById(R.id.button2);
        changeDate2 = (Button) findViewById(R.id.button3);
        spinner2=(Spinner) findViewById(R.id.spinner2);
        btn1=(Button) findViewById(R.id.button1);
        e1=(EditText) findViewById(R.id.editText1);
        addItemsOnSpinner2();
        rg2=(RadioGroup) findViewById(R.id.radiogroup2);
        rg2.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {

            @Override
            public void onCheckedChanged(RadioGroup arg0, int arg1) {
                // TODO Auto-generated method stub

                switch(arg1)
                {
                case R.id.radioButton5:
                    flag1=1;
                    break;
                case R.id.radioButton6:
                    flag2=1;
                    break;
                }
            }



        });
        spinner2.setOnItemSelectedListener(new MySpinnerSelectedListener());
        // Get current date by calender

        /*final Calendar c = Calendar.getInstance();
        year  = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day   = c.get(Calendar.DAY_OF_MONTH);*/

        final Calendar c = Calendar.getInstance();
        year=c.YEAR;
        month=c.MONTH+1;
        day=c.DAY_OF_MONTH;

        // Button listener to show date picker dialog


        changeDate1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                // On button click show datepicker dialog 
                showDialog(DATE_PICKER_ID1);

            }

        });
        changeDate2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // On button click show datepicker dialog 
                showDialog(DATE_PICKER_ID2);

            }

        });


     btn1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
            if(e1.getText().length()==0 || (flag1==0 && flag2==0))
            {
                Toast.makeText(getBaseContext(), "Please Enter all the fields!!",Toast.LENGTH_LONG).show();
            }
            else if(flag1==1)
            {
                Intent i=new Intent(LeaveApp.this,TestSelect.class);
                i.putExtra("data1", spinner2.getSelectedItem().toString());
                startActivity(i);
            }
            }
        });
   }
 public void addItemsOnSpinner2() {
        // TODO Auto-generated method stub
        spinner2=(Spinner) findViewById(R.id.spinner2);
        List<String> list2 = new ArrayList<String>();
        list2.add("I");
        list2.add("II");
        list2.add("III");
        list2.add("IV");
        list2.add("V");
        list2.add("VI");
        list2.add("VII");
        list2.add("VIII");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list2);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(dataAdapter);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_PICKER_ID1:


            // set date picker for current date 

            return new DatePickerDialog(this, pickerListener1, year, month,day);


       case DATE_PICKER_ID2:


            // set date picker for current date 

            return new DatePickerDialog(this, pickerListener2, year, month,day);


        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener pickerListener1 = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        @Override
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {

            year  = selectedYear;
            month = selectedMonth;
            day   = selectedDay;

            // Show selected date 
         /*   Output1.setText(new StringBuilder().append(month + 1)
                    .append("-").append(day).append("-").append(year)
                    .append(" "));*/
            Output1.setText(new StringBuilder().append(day).append("-").append(month + 1)
                    .append("-").append(year).append(" "));


           }
        };
        private DatePickerDialog.OnDateSetListener pickerListener2 = new DatePickerDialog.OnDateSetListener() {

            // when dialog box is closed, below method will be called.
            @Override
            public void onDateSet(DatePicker view, int selectedYear,
                    int selectedMonth, int selectedDay) {

                year  = selectedYear;
                month = selectedMonth;
                day   = selectedDay;

                // Show selected date 

                Output2.setText(new StringBuilder().append(month + 1)
                        .append("-").append(day).append("-").append(year)
                        .append(" "));

               }
            };
}

public class TestSelect extends Activity {

 private Spinner spinner3,spinner2;
 private Button btn4;
 CharSequence[] options;
 boolean[] selections;
 String sem;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.testselect);
    //spinner3=(Spinner) findViewById(R.id.spinner3);
    spinner2=(Spinner) findViewById(R.id.spinner2);
     Intent in=getIntent();
    sem=in.getStringExtra("data1");
    //addItemsOnSpinner3();
    addItemsOnButton();
    //System.out.println(sem);
    btn4=(Button) findViewById(R.id.button4);
    btn4.setOnClickListener(new ButtonClickHandler()  );
}
public class ButtonClickHandler implements View.OnClickListener {
    public void onClick( View view ) {
        showDialog( 0 );
    }
}

private void addItemsOnButton() {
if(sem.equals("III"))
    {

        options=new CharSequence[]{"DSPD","CAO","BDP"};
         selections =  new boolean[ options.length ];
    }
    else if(sem.equals("IV"))
    {

        options=new CharSequence[] {"TOFCS","OOP","OS"};
        selections =  new boolean[ options.length ];
    }
    else if(sem.equals("V"))
    {   
                    options=new CharSequence[] {"MI","CG","SP"};
        selections =  new boolean[ options.length ];
    }
    else //if(spinner2.getSelectedItem()=="VI")
    {

        options=new CharSequence[] {"DAA","DBMS","ICWS"};
        selections =  new boolean[ options.length ];
    }
}
@Override
protected Dialog onCreateDialog( int id ) 
{
    return 
    new AlertDialog.Builder( this )
        .setTitle( "SUBJECTS" )
        .setMultiChoiceItems( options, selections, new DialogSelectionClickHandler() )
        .setPositiveButton( "OK", new DialogButtonClickHandler() )
        .create();
}
public class DialogSelectionClickHandler implements DialogInterface.OnMultiChoiceClickListener
{
    public void onClick( DialogInterface dialog, int clicked, boolean selected )
    {
        Log.i( "ME", options[ clicked ] + " selected: " + selected );
    }
}

public class DialogButtonClickHandler implements DialogInterface.OnClickListener
{
    public void onClick( DialogInterface dialog, int clicked )
    {
        switch( clicked )
        {
            case DialogInterface.BUTTON_POSITIVE:
                printSelectedSubjects();
                break;
        }
    }

    private void printSelectedSubjects() {
        // TODO Auto-generated method stub
        for( int i = 0; i < options.length; i++ ){
            Log.i( "ME", options[ i ] + " selected: " + selections[i] );
        }
    }
}

}

点击按钮,它应该从LeaveApp到TestSelect选择特定的radiobutto。请帮忙。

leaveapp.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="true"
    android:orientation="vertical" 
    android:layout_marginLeft="26dp"
        android:layout_marginTop="20dp">

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="380dp"
            android:orientation="vertical" >
              <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LEAVE TYPE"
        android:textAppearance="?android:attr/textAppearanceMedium" />
               <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

  <RadioGroup
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/radiogroup2" >

    <RadioButton
        android:id="@+id/radioButton5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEST"
         android:layout_marginTop="10dp" />


    <RadioButton
        android:id="@+id/radioButton6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CASUAL"
         android:layout_marginTop="10dp" />
    </RadioGroup>
     </LinearLayout>

       <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SEM"
        android:textAppearance="?android:attr/textAppearanceMedium" 
         android:layout_marginTop="10dp" />
    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:layout_marginLeft="50dp"  />
     </LinearLayout>
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

     <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="REASON"
        android:layout_marginTop="10dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />
       <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_marginTop="10dp"
        android:inputType="textPostalAddress" >

        <requestFocus />
       </EditText>
      </LinearLayout>
      <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LEAVE DURATION"
        android:layout_marginTop="10dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />
      <RelativeLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent" >
       <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="FROM"
        android:layout_marginTop="10dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

        <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="SELECT" />

       <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_marginLeft="26dp"
        android:layout_toRightOf="@+id/textView5"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />

       </RelativeLayout>

      <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent" >

          <RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_alignParentLeft="true"
              android:layout_alignParentTop="true" 
              android:layout_marginTop="10dp">

              <TextView
                  android:id="@+id/textView6"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_marginTop="50dp"
                  android:text="TO"
                  android:textAppearance="?android:attr/textAppearanceMedium" />

              <TextView
                  android:id="@+id/textView8"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentBottom="true"
                  android:layout_marginLeft="38dp"
                  android:layout_toRightOf="@+id/textView6"
                  android:text=""
                  android:textAppearance="?android:attr/textAppearanceMedium" />

              <Button
                  android:id="@+id/button3"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentBottom="true"
                  android:layout_alignParentRight="true"
                  android:text="SELECT" />

          </RelativeLayout>

           </RelativeLayout>

    <Button
         android:id="@+id/button1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginLeft="100dp"
         android:layout_marginTop="20dp"
         android:text="NEXT" />

        </LinearLayout>
       </ScrollView>


    </LinearLayout>


logcat


 11-09 00:51:26.302: D/AndroidRuntime(1483): Shutting down VM
11-09 00:51:26.302: W/dalvikvm(1483): threadid=1: thread exiting with uncaught exception (group=0xb4b16b90)
11-09 00:51:26.392: E/AndroidRuntime(1483): FATAL EXCEPTION: main
11-09 00:51:26.392: E/AndroidRuntime(1483): Process: com.ara.quickaccess, PID: 1483
11-09 00:51:26.392: E/AndroidRuntime(1483): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ara.quickaccess/com.ara.quickaccess.TestSelect}: java.lang.NullPointerException
11-09 00:51:26.392: E/AndroidRuntime(1483):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
11-09 00:51:26.392: E/AndroidRuntime(1483):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
11-09 00:51:26.392: E/AndroidRuntime(1483):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
11-09 00:51:26.392: E/AndroidRuntime(1483):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
11-09 00:51:26.392: E/AndroidRuntime(1483):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-09 00:51:26.392: E/AndroidRuntime(1483):     at android.os.Looper.loop(Looper.java:137)
11-09 00:51:26.392: E/AndroidRuntime(1483):     at android.app.ActivityThread.main(ActivityThread.java:4998)
11-09 00:51:26.392: E/AndroidRuntime(1483):     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 00:51:26.392: E/AndroidRuntime(1483):     at java.lang.reflect.Method.invoke(Method.java:515)
11-09 00:51:26.392: E/AndroidRuntime(1483):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-09 00:51:26.392: E/AndroidRuntime(1483):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-09 00:51:26.392: E/AndroidRuntime(1483):     at dalvik.system.NativeStart.main(Native Method)
11-09 00:51:26.392: E/AndroidRuntime(1483): Caused by: java.lang.NullPointerException
11-09 00:51:26.392: E/AndroidRuntime(1483):     at com.ara.quickaccess.TestSelect.<init>(TestSelect.java:28)
11-09 00:51:26.392: E/AndroidRuntime(1483):     at java.lang.Class.newInstanceImpl(Native Method)
11-09 00:51:26.392: E/AndroidRuntime(1483):     at java.lang.Class.newInstance(Class.java:1208)
11-09 00:51:26.392: E/AndroidRuntime(1483):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
11-09 00:51:26.392: E/AndroidRuntime(1483):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
11-09 00:51:26.392: E/AndroidRuntime(1483):     ... 11 more

1 个答案:

答案 0 :(得分:0)

请检查EditText&#34; e1&#34;的对象如

e1=(EditText) findViewById(R.id.textView1);

textView1TextViewEditTex t。

的ID

根据您的logcat,ID textView1不是EditText所以只需替换

  e1=(EditText) findViewById(R.id.textView1);

 e1=(TextView)findViewById(R.id.textView1);

或将ID textView1设为EditText