我正在创建我的第一个应用程序,我不知道为什么,当我点击“新游戏”时,我收到通知“appka已停止”。另外我有疑问是我对sharedPreferences的增量是否正确?
spring-boot:stop
和.xml:
public class MainActivity extends ActionBarActivity {
TextView textView, textView2, solution;
int MyNumber, userNumber, guessCont;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView2);
textView2 = (TextView)findViewById(R.id.textView3);
solution = (TextView)findViewById(R.id.textView4);
sharedPreferences = getSharedPreferences("com.example.lewan.appka", Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void NewGame(View view){
Random rand = new Random();
MyNumber = rand.nextInt((5-0)+1)+0;
guessCont =0;
textView.setText("Time guessed: " + guessCont);
solution.setText(MyNumber);
}
public void guess(View view) {
guessCont++;
EditText editText =(EditText)findViewById(R.id.editText);
userNumber = Integer.parseInt(editText.getText().toString());
String message = "";
if(userNumber!=MyNumber){
message = "wrong number";
}
else if(userNumber==MyNumber){
message = "correct number";
int i=0;
editor.putInt("Amount of games",i++);
editor.commit();
textView2.setText("Amount of games: " + sharedPreferences);
}
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, message, duration);
toast.show();
toast.setGravity(Gravity.CENTER, 0, 0);
textView.setText("Time guessed: " + guessCont);
}
}
bla bla bla,我的帖子主要是代码我需要更多bla bla
编辑:来自日志的错误
<EditText
android:layout_width="120dp"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="75dp"
android:layout_marginLeft="50dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Guess!"
android:id="@+id/button2"
android:layout_alignBottom="@+id/editText"
android:layout_toRightOf="@+id/editText"
android:layout_toEndOf="@+id/editText"
android:onClick="guess"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Game"
android:id="@+id/button3"
android:layout_below="@+id/editText"
android:layout_alignLeft="@+id/editText"
android:layout_alignStart="@+id/editText"
android:layout_alignRight="@+id/button2"
android:layout_alignEnd="@+id/button2"
android:onClick="NewGame"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time Guess"
android:id="@+id/textView2"
android:layout_below="@+id/button3"
android:layout_alignLeft="@+id/button3"
android:layout_alignStart="@+id/button3"
android:layout_marginTop="32dp"
android:textSize="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Amount of games"
android:id="@+id/textView3"
android:textSize="20dp"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Solution (for testing)"
android:id="@+id/textView4"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
答案 0 :(得分:1)
我认为改变代码
textView2.setText("Amount of games: " + sharedPreferences);
进入
textView2.setText("Amount of games: " + sharedPreferences.getString("Amount of games","");
答案 1 :(得分:1)
基本上,您对Textview上设置的文本使用了 Integer 值,因此您必须使用 String 进行类型转换。
solution.setText(MyNumber);
到
solution.setText(""+MyNumber);
希望这会对你有所帮助。