Android OnActivityResult setVisibility

时间:2012-08-29 00:23:03

标签: android visibility

我在同一个活动中有两个按钮。每个按钮调用RecognizerIntent进行一些语音识别。如果让麦克风图标Image1和Image2变得不可见或者如果说出正确的单词就会消失,我试图完成的任务。

public class Cooking  extends Activity implements OnClickListener{


    ListView lv;
    static final int check = 1;
    static final int checka = 2;


    int counter_score;
     int counter_score_b;
    TextView txView;
    MediaPlayer ourSong;
    ImageView display,image1,image2;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        counter_score=0;
        counter_score_b=0;
        setContentView(R.layout.cooking);
        ImageView image1 = (ImageView) findViewById(R.id.image_1_wrong);
        ImageView image2 = (ImageView) findViewById(R.id.image_2_wrong);        
        initialize();





}


private void initialize() {
    // TODO Auto-generated method stub
    lv = (ListView)findViewById(R.id.lvVoiceReturn);
    txView = (TextView)findViewById(R.id.counter);

    ImageView image1 = (ImageView) findViewById(R.id.image_1_wrong);
    ImageView image2 = (ImageView) findViewById(R.id.image_2_wrong);

    image1.setOnClickListener(this);
    image2.setOnClickListener(this);


    ActionBar actionBar = getActionBar();
     actionBar.setDisplayHomeAsUpEnabled(false);

}




    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.IVimage_1_wrong){
        Intent a = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        a.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        a.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
        a.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak");
        startActivityForResult(a, checka);
    }else if(v.getId() == R.id.IVimage_2_wrong){
        Intent b = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        b.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        b.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
        b.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak.");
        startActivityForResult(b, checkb);
    }
}


 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (requestCode == checka && resultCode == RESULT_OK){
            ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));
            if(results.contains("cooking")|| results.contains("yes really") || results.contains("disease")){

                counter_score ++;
                    ///////I though this would make ImageView Image1 not be visible  
                image1.setVisibility(View.GONE);}
                ourSong = MediaPlayer.create(Cooking.this, R.raw.rightsound2);
                ourSong.start();
                AlertDialog dialogBuilder = new AlertDialog.Builder(this).create();
                dialogBuilder.setTitle("Good");dialogBuilder.setMessage("Correct");
                dialogBuilder.setIcon(R.drawable.ic_mark);
                dialogBuilder.show();
                Thread timer = new Thread(){
                    public void run(){
                        try{
                         sleep(2500);
                        }catch (InterruptedException e){
                            e.printStackTrace();
                        } finally {





                        }
                    }
                };
                timer.start();

            }else{

                lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));
                counter_score +=0;

                counter_score_z +=0;
                ourSong = MediaPlayer.create(Cooking.this, R.raw.wrongsound);
                ourSong.start();
                AlertDialog dialogBuilder = new AlertDialog.Builder(this).create();
                dialogBuilder.setTitle("Oops");
                dialogBuilder.setMessage("Error msg");
                dialogBuilder.setIcon(R.drawable.ic_wrong);
                dialogBuilder.setButton("Continue", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {


                        Intent openMovilTeacherApp = new Intent(Cooking.this, LetterEx.class);
                        startActivity(openMovilTeacherApp);
                        finish();
                    }
                });
                dialogBuilder.setButton2("Try again", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });
                dialogBuilder.show();
                Thread timer = new Thread(){
                    public void run(){
                        try{
                         sleep(4000);
                        }catch (InterruptedException e){
                            e.printStackTrace();
                        } finally {



                        }
                    }
                };
                timer.start();
            }



        }
        //////Delivering MAIL/////
        if (requestCode == checkb && resultCode == RESULT_OK){
            ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));
            if(results.contains("delivering mail")|| results.contains("no not really") || results.contains("disease")){

                counter_score ++ ;
                 ///////// the next line was supposed to make the image2 no be visible, or gone
                image2.setVisibility(View.GONE);}
                counter_score_z +=1;
                ourSong = MediaPlayer.create(Cooking.this, R.raw.rightsound2);
                ourSong.start();
                AlertDialog dialogBuilder = new AlertDialog.Builder(this).create();
                dialogBuilder.setTitle("Good");dialogBuilder.setMessage("Correct");
                dialogBuilder.setIcon(R.drawable.ic_mark);
                dialogBuilder.show();
                Thread timer = new Thread(){
                    public void run(){
                        try{
                         sleep(2500);
                        }catch (InterruptedException e){
                            e.printStackTrace();
                        } finally {
                            Intent nextAct2 = new Intent(Cooking.this, LetterEx.class);
                            startActivity(nextAct2);
                            finish();



                        }
                    }
                };
                timer.start();

            }else{

                lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));
                counter_score +=0;
                txView.setId(0);
                counter_score_z +=0;
                ourSong = MediaPlayer.create(Cooking.this, R.raw.wrongsound);
                ourSong.start();
                AlertDialog dialogBuilder = new AlertDialog.Builder(this).create();
                dialogBuilder.setTitle("Oops,");
                dialogBuilder.setMessage("Error Msg");
                dialogBuilder.setIcon(R.drawable.ic_wrong);
                dialogBuilder.setButton("Continuar", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {


                        Intent openMovilTeacherApp = new Intent(Cooking.this, LetterEx.class);
                        startActivity(openMovilTeacherApp);
                        finish();
                    }
                });
                dialogBuilder.setButton2("Try again", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });
                dialogBuilder.show();
                Thread timer = new Thread(){
                    public void run(){
                        try{
                         sleep(4000);
                        }catch (InterruptedException e){
                            e.printStackTrace();
                        } finally {



                        }
                    }
                };
                timer.start();
            }



        }



        super.onActivityResult(requestCode, resultCode, data);



    }

}

我的烹饪文件夹

<ImageView android:src="@drawable/small_right_blue_mic"
        android:id="@+id/image_1_wrong" 
       android:layout_width="68dp"
        android:layout_height="68dp"
        android:layout_marginLeft="-150dp"
        android:layout_marginTop="15dp"
        android:visibility="invisible"/>

     <ImageView android:src="@drawable/small_right_blue_mic"
        android:id="@+id/image_2_wrong" 
       android:layout_width="52dp"
        android:layout_height="59dp"
        android:layout_marginLeft="-0dp"
        android:layout_marginTop="25dp"
        android:visibility="visible"/>

错误日志

08-29 17:15:54.600: E/AndroidRuntime(1953): FATAL EXCEPTION: main
08-29 17:15:54.600: E/AndroidRuntime(1953): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=222, result=-1, data=Intent { (has extras) }} to activity {com.testapp.app/com.testapp.app.Cooking}: java.lang.NullPointerException

1 个答案:

答案 0 :(得分:0)

您的问题是您在本地定义了image1和image2。像这样改变它。而且你只需要使用一次findviewbyid()。那就是

public class Cooking  extends Activity implements OnClickListener{


    ListView lv;
    static final int check = 1;
    static final int checka = 2;


    int counter_score;
     int counter_score_b;
    TextView txView;
    MediaPlayer ourSong;
    ImageView display,image1,image2;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        counter_score=0;
        counter_score_b=0;
        setContentView(R.layout.cooking);
        image1 = (ImageView) findViewById(R.id.image_1_wrong);
        image2 = (ImageView) findViewById(R.id.image_2_wrong);        
        initialize();





}


private void initialize() {
    // TODO Auto-generated method stub
    lv = (ListView)findViewById(R.id.lvVoiceReturn);
    txView = (TextView)findViewById(R.id.counter);


    image1.setOnClickListener(this);
    image2.setOnClickListener(this);


    ActionBar actionBar = getActionBar();
     actionBar.setDisplayHomeAsUpEnabled(false);

}