Я создаю свое первое приложение, и я не знаю, почему, когда я нажимаю «Новая игра», я получаю уведомление «appka остановился». Также у меня вопрос, верно ли мое добавление sharedPreferences?Мое простое приложение остановилось и вышло с инкрементами в sharedPreferences
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);
}
}
и .xml:
<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" />
бла-бла-бла, мой пост в основном код мне нужно некоторые более бла-бла
EDIT: ошибки из журнала
08-05 10:44:03.538 29161-29161/com.example.lewan.appka E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3606)
at android.view.View.performClick(View.java:4211)
at android.view.View$PerformClick.run(View.java:17367)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5227)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3601)
at android.view.View.performClick(View.java:4211)
at android.view.View$PerformClick.run(View.java:17367)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5227)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x3
at android.content.res.Resources.getText(Resources.java:230)
at android.widget.TextView.setText(TextView.java:3769)
at com.example.lewan.appka.MainActivity.NewGame(MainActivity.java:65)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3601)
at android.view.View.performClick(View.java:4211)
at android.view.View$PerformClick.run(View.java:17367)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5227)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
Я считаю, что вы получаете значения нежелательной почты в текстовом редакторе textView2 – Kedarnath
@HelicoP, где вы называете функции NewGame и Guess()? –
Я не знаю, что вы имеете в виду - я просто добавляю «onclick» в кнопки, а затем создаю для этого методы. Мне еще есть что добавить? – HelicoP