使用Button提交信息时出错

时间:2016-05-30 05:42:43

标签: android android-studio button android-spinner

我有很多微调器,用户需要从中选择值。那些是spinner1,spinner2,s1,s2,s3。 单击按钮 add 时,应该将微调器上的字符串传递到数据库中。但是在这里我用吐司替换它因为我仍然有错误,所以我用吐司测试它。

问题是应用程序会说不幸的是,__已停止,错误显示它与行[select* question-201 "Can you fix this?" "How much do repairs cost?" "Did you receive my order?" "When will my order be ready?" "Other"]

一致

以下是完整代码:

add.setOnClickListener(this);

错误logcat

public class try_spinner extends Activity implements AdapterView.OnItemSelectedListener, View.OnClickListener {
Spinner spinner1, sspinner2, s1, s2,s3;
String app_name,room_name, spinner2_text, s1_text,s2_text, s3_text;
Button add, cancel;
TextView app,room, text_s1,text_s2,text_s3;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.try_spinner);

    DisplayMetrics dm=new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int width=dm.widthPixels;
    int height=dm.heightPixels;

    getWindow().setLayout((int) (width * .9), (int) (height * .7));

    Bundle extras = getIntent().getExtras();
    final String app_string = extras.getString("APPLIANCE_NAME");
    final String room_string = extras.getString("APPLIANCE_ROOM");

    app =(TextView)findViewById(R.id.app_name);
    app.setText(app_string);
    room =(TextView)findViewById(R.id.room_name);
    room.setText(room_string);
    text_s1 =(TextView)findViewById(R.id.text_s1);
    text_s2 =(TextView)findViewById(R.id.text_s2);
    text_s3 =(TextView)findViewById(R.id.text_s3);

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

    if(app_string.equals("Lighting")){
        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.lighting));
    dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataAdapter2.notifyDataSetChanged();
        spinner1.setAdapter(dataAdapter2);}
    else if(app_string.equals("Shower Heater")) {
        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.shower_heater));
        dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataAdapter2.notifyDataSetChanged();
        spinner1.setAdapter(dataAdapter2);}
    else {
        ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.empty));
        dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        dataAdapter2.notifyDataSetChanged();
        spinner1.setAdapter(dataAdapter2);}

    sspinner2 = (Spinner) findViewById(R.id.spinner2);
    s1 = (Spinner) findViewById(R.id.s1);
    s2 = (Spinner) findViewById(R.id.s2);
    s3 = (Spinner) findViewById(R.id.s3);
    sspinner2.setOnItemSelectedListener(this);
    s1.setOnItemSelectedListener(this);
    s2.setOnItemSelectedListener(this);
    s3.setOnItemSelectedListener(this);


    final Button add = (Button) findViewById(R.id.add_info);
    add.setOnClickListener(this);
    final Button cancel = (Button) findViewById(R.id.cancel_add);
    cancel.setOnClickListener(this);

}

@Override
public void onClick(View v) {       //need intent status
// TODO Auto-generated method stub

    switch (v.getId()) {
        case R.id.add_info:
            if (spinner2_text.contentEquals("Daily")) {
                Toast.makeText(getBaseContext(), "its daily" + s1_text +s2_text, Toast.LENGTH_LONG).show();
            } else if (spinner2_text.contentEquals("Monthly")) {
                Toast.makeText(getBaseContext(), "its daily" + s1_text +s2_text, Toast.LENGTH_LONG).show();
            }break;
        case R.id.cancel_add:
            Toast.makeText(getBaseContext(), "canceling", Toast.LENGTH_LONG).show();
            break;
    }

}

public void onItemSelected(AdapterView<?> parent, View view, int position,
                           long id) {
    Spinner clickedSpinner = (Spinner) parent;
    switch (clickedSpinner.getId()) {
        case R.id.spinner2:
            spinner2_text = String.valueOf(sspinner2.getSelectedItem());
            Toast.makeText(this, spinner2_text, Toast.LENGTH_SHORT).show();
            if (spinner2_text.contentEquals("Daily")) {
                ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this,
                        android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.hours));
                dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                dataAdapter1.notifyDataSetChanged();
                s1.setAdapter(dataAdapter1);
                text_s1.setText("hour(s)");
                s1_text = String.valueOf(s1.getSelectedItem());

                ArrayAdapter<String> dataAdapter6 = new ArrayAdapter<String>(this,
                        android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.minutes));
                dataAdapter6.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                dataAdapter6.notifyDataSetChanged();
                s2.setAdapter(dataAdapter6);
                text_s2.setText("min(s)");
                s2_text = String.valueOf(s2.getSelectedItem());

                s3.getSelectedView();
                s3.setEnabled(false);

                text_s3.setText("");
                Toast.makeText(getBaseContext(), "its daily" + s1_text +s2_text, Toast.LENGTH_LONG).show();
            }else if(spinner2_text.contentEquals("Monthly")) {
                ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this,
                        android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.days));
                dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                dataAdapter3.notifyDataSetChanged();
                s1.setAdapter(dataAdapter3);
                text_s1.setText("day(s)");
                s1_text = String.valueOf(s1.getSelectedItem());

                ArrayAdapter<String> dataAdapter4 = new ArrayAdapter<String>(this,
                        android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.hours));
                dataAdapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                dataAdapter4.notifyDataSetChanged();
                s2.setAdapter(dataAdapter4);
                text_s2.setText("hour(s)");
                s2_text = String.valueOf(s2.getSelectedItem());

                s3.getSelectedView();
                s3.setEnabled(true);
                ArrayAdapter<String> dataAdapter5 = new ArrayAdapter<String>(this,
                        android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.minutes));
                dataAdapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                dataAdapter5.notifyDataSetChanged();
                s3.setAdapter(dataAdapter5);
                text_s3.setText("min(s)");
                s3_text = String.valueOf(s3.getSelectedItem());
                Toast.makeText(getBaseContext(), "its monthly" + s1_text +s2_text+ s3_text,Toast.LENGTH_LONG).show();
            }
            break;
        default:
            break;
    }
}

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

}
}

try_spinner.xml

05-30 01:35:53.978 3778-3778/? E/AndroidRuntime: FATAL EXCEPTION: main
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shyan.electricconsumer160326/com.example.shyan.electricconsumer160326.try_spinner}: java.lang.NullPointerException
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5103)
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:525)
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:  Caused by: java.lang.NullPointerException
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at com.example.shyan.electricconsumer160326.try_spinner.onCreate(try_spinner.java:180)
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5133)
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:141) 
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99) 
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137) 
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5103) 
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:525) 
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
05-30 01:35:53.978 3778-3778/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 

1 个答案:

答案 0 :(得分:0)

id添加到添加按钮

 <Button
    android:id="@+id/add_info"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:text="Add"
    android:layout_gravity="center_vertical"
    android:background="#394141"
    android:layout_margin="5dp"
    android:textColor="#FFFFFF" />