-1

我正在制作一个简单的应用程序,你输入你的名字,点击一个按钮,选择一个爱好,然后它会回到你的名字和爱好一起的主布局。当我单击“取消(取消)”按钮时遇到问题,应用程序不断收到致命错误。你觉得它可能是什么?

主要活动:

package com.example.holaamigos;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

    public class MainActivity extends Activity {
        public final static String EXTRA_SALUDO = "com.example.holaamigos.SALUDO";

    TextView txtfinal;
    String saludo;

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

        final EditText txtNombre = (EditText)findViewById(R.id.TxtNombre);
        final Button btnHola = (Button)findViewById(R.id.BtnHola);
        final CheckBox checkbox1 =(CheckBox)findViewById(R.id.checkBox1);
        txtfinal=(TextView)findViewById(R.id.textView1);

            checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener(){

                @Override
                public void onCheckedChanged(CompoundButton arg0,
                        boolean checked) {
                    if (checked)
                        {
                        Toast.makeText(checkbox1.getContext(), "Activo", Toast.LENGTH_LONG).show();
                        btnHola.setVisibility(0);
                        btnHola.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(MainActivity.this, ActivitySaludo.class);
                                saludo = txtNombre.getText().toString();
                                intent.putExtra(EXTRA_SALUDO, saludo);
                                startActivityForResult(intent, 1);
                            }
                        });

                        }
                    else 
                    {           
                        Toast.makeText(checkbox1.getContext(), "Inactivo", Toast.LENGTH_SHORT).show();
                        btnHola.setVisibility(View.INVISIBLE);
                    }
                }
            });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK);
        String string= data.getStringExtra("HOBBY");
        txtfinal.setText("Nombre:" + saludo + "   " + "Hobby:" + string);

        if (resultCode == RESULT_CANCELED){

        }

    }

    @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;
    }

}

活动Saludo:

package com.example.holaamigos;




import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class ActivitySaludo extends Activity {
    String myspinner;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_saludo);


        Intent intent = getIntent();
        String saludo = intent.getStringExtra(MainActivity.EXTRA_SALUDO);

        TextView txtCambiado = (TextView) findViewById(R.id.TxtSaludo);
        txtCambiado.setText(getString(R.string.hola_saludo) + " " + saludo);

        final Spinner spinner = (Spinner)findViewById(R.id.SpinnerSaludo);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.hobby, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new OnItemSelectedListener () {


            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int pos, long id) {
                parent.getItemAtPosition(pos);
                myspinner = spinner.getItemAtPosition(pos).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                //another call

            }

        });

        final Button BtnAceptar=(Button) findViewById(R.id.buttonAceptar);
        BtnAceptar.setOnClickListener(new OnClickListener (){

            @Override
            public void onClick(View v) {
                Intent iboton = new Intent();   
                iboton.putExtra("HOBBY", myspinner);
                setResult(RESULT_OK, iboton);
                finish();
            }


        });

        final Button BtnCancelar=(Button) findViewById(R.id.buttonCancelar);
        BtnCancelar.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                Intent iboton2 = new Intent();
                iboton2 = null;
                setResult(RESULT_CANCELED, iboton2);
                finish();
            }

        });

    }

}
4

1 回答 1

0
if (resultCode == RESULT_OK);
String string= data.getStringExtra("HOBBY");
txtfinal.setText("Nombre:" + saludo + "   " + "Hobby:" + string);

这就是错误所在..您需要;if语句末尾删除并放在{那里,并以同样的方式结束块}

于 2013-10-25T12:54:47.053 回答