简体   繁体   English

当我按回按钮时,我的应用程序停止工作

[英]When I press the button back, my application stops working

Good afternoon, I have an application that has an activity that is a menu and communicates with 2 other activities (one to choose a name from a list and the other to choose an exercise).下午好,我有一个应用程序,它有一个菜单活动,并与其他 2 个活动(一个从列表中选择名称,另一个选择练习)进行通信。 The passing of data between the menu and these activities works correctly.菜单和这些活动之间的数据传递工作正常。 The problem I have is that if the user clicks the back button instead of choosing a name from the list, for example, the application stops working and in the logcat I get an error.我遇到的问题是,例如,如果用户单击后退按钮而不是从列表中选择名称,则应用程序将停止工作并且在 logcat 中出现错误。 in the line of the menu activity where I have the getStringExtra ();在我有 getStringExtra () 的菜单活动行中;

If someone knows how to solve it, I would really appreciate it.如果有人知道如何解决它,我将不胜感激。

This is the code of de menu activity这是菜单活动的代码

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

    tv1=(TextView)findViewById(R.id.exercicitext);
    tv2=(TextView)findViewById(R.id.jugadortext);

    botoex = (Button)findViewById(R.id.botoexercici);
    botoex.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openExercici();
        }
    });

    botojug = (Button)findViewById(R.id.botojugador);
    botojug.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LlistaJugadors();
        }
    });

    botoini = (Button)findViewById(R.id.botoinici);
    botoini.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        public void onClick(View v) {
            openCountdown();
        }
    });

    afegirjug = (Button)findViewById(R.id.botoafegirjugador);
    afegirjug.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Afegirjugadors();
        }
    });
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void openCountdown(){
    Intent intent = new Intent(this,countdown.class);
    if(Objects.equals(dada1,"Test Hexàgon")){
        intent.putExtra("exercici", "1");
        intent.putExtra("nom",dada2);
    }
    else if(Objects.equals(dada1,"Test Seqüència")){
        intent.putExtra("exercici", "2");
        intent.putExtra("nom",dada2);
    }
    startActivity(intent);
}

public void openExercici(){
    Intent intent = new Intent(this,Hex_Seq.class);
    startActivityForResult(intent,1);
}

public void Afegirjugadors(){
    Intent intent = new Intent(this,Afegirjug.class);
    startActivity(intent);
}

public void LlistaJugadors(){
 Intent intent = new Intent(this,Llistajug.class);
 startActivityForResult(intent,2);
}

public void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode,resultCode,data);
                                                                
    if(requestCode == 1){
        dada1 = data.getStringExtra("dato1");
        tv1.setText("Exercici - "+dada1);
        u=u+1;
    }
    else if(requestCode == 2){
        dada2 = data.getStringExtra("dato2");
        tv2.setText("Jugador - "+dada2);
        e=e+1;
    }
    if((u>=1)&&(e>=1)){
        botoini.setEnabled(true); //Asigna valor false.
    }
}

} ` } `

And this is the code that shows the list of names这是显示名称列表的代码

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

public void Llistajugadors(){
    AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this,"administracio",null,1);
    SQLiteDatabase BaseDeDades = admin.getWritableDatabase();
    if(BaseDeDades!=null){
        Cursor c= BaseDeDades.rawQuery("select * from jugadors",null);
        int quantitat = c.getCount();
        int i=0;
        String[] array = new String[quantitat];
        if (c.moveToFirst()){
            do{

                String linia = c.getInt(0)+"-"+c.getString(1);

                array[i] = linia;
                i++;

            }while(c.moveToNext());
        }
        ArrayAdapter<String>adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,array);
        final ListView llista = (ListView)findViewById(R.id.llista);
        llista.setAdapter(adapter);

        llista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = getIntent();
                intent.putExtra("dato2", llista.getItemAtPosition(position).toString());
                setResult(RESULT_OK,intent);

                finish();
            }
        });
    }
}

} }

In your onActivutyResult() you have to check if your result code is ok, if someone doesn't select anything it throws back nothing try this:在您的 onActivutyResult() 中,您必须检查您的结果代码是否正常,如果有人没有 select 任何它不会抛出任何东西试试这个:

public void onActivityResult(int requestCode, int 
 resultCode, Intent data){
    super.onActivityResult(requestCode,resultCode,data);
       If(result code== RESULT_OK){                                                     
         if(requestCode == 1){
             dada1 = data.getStringExtra("dato1");
             tv1.setText("Exercici - "+dada1);
             u=u+1;
           }
         else if(requestCode == 2){
              dada2 = data.getStringExtra("dato2");
              tv2.setText("Jugador - "+dada2);
              e=e+1;
          }
       }
        if((u>=1)&&(e>=1)){
            botoini.setEnabled(true); //Asigna valor false.
         }
       
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM