我的代码有问题。应用程序的基本任务是获取一个随机数,然后用户必须猜测它。当我试图运行该应用程序时出现了一些错误,我正在寻找一个错字,但我不明白为什么它不起作用。问题出在" setText"属性附加伤害。这是我的MainActivity.java文件:
package pl.zgadula.zgadnijliczbe;
import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast;
import java.util.Random;
import static android.R.id.message; import static pl.zgadula.zgadnijliczbe.R.id.strzal; import static pl.zgadula.zgadnijliczbe.R.id.textView;
public class MainActivity extends AppCompatActivity {
int myNumber, userNumber, guessCount=0 ;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
}
public void newGame(View view) {
Random rand = new Random();
myNumber = rand.nextInt((100-0)+1)+0;
guessCount = 0;
textView = (TextView)findViewById(R.id.textView);
textView.setText("Ile razy zgadywałeś: " + guessCount);
}
public void takeTheGuess(View view) {
guessCount++;
EditText editText = (EditText)findViewById(R.id.editText);
userNumber = Integer.parseInt(editText.getText().toString());
String message = "";
if(userNumber>myNumber){
message = "Moja liczba jest mniejsza niż Twoja";
}
else if (userNumber<myNumber){
message = "Moja liczba jest większa od Twojej";
}
else if (userNumber==myNumber){
message = "Gratulacje! To poprawna liczba!";
}
}
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, message, duration);
toast.show();
TextView textView = (TextView)findViewById(R.id.textView);
textView.setText("Ile razy zgadywałeś: " + guessCount)
}
答案 0 :(得分:-1)
以下行应该是方法的一部分,但是您已经在方法之外编写了它们:
Toast toast = Toast.makeText(context, message, duration);
toast.show();
TextView textView = (TextView)findViewById(R.id.textView);
textView.setText("Ile razy zgadywałeś: " + guessCount);