请帮我弄清楚代码的问题。首先应用程序按预期工作,但当屏幕方向从纵向更改为横向应用程序正在销毁时,这就是为什么我决定添加'onSaveInstanceState(Bundle outState)'和'onRestoreInstanceState(Bundle savedInstanceState)',我真的Java中的新功能,但我试着自己解决了四天而没有实际结果。
以下是我的Java代码和logcat:
package com.example.android.dartscounter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int scorePlayerOne = 0;
int scorePlayerTwo = 0;
NumberPicker noPicker;
NumberPicker niPicker;
int currentValue = 0;
int currentValue2 = 0;
static final String STATE_SCORE_ONE = "myInt";
static final String STATE_SCORE_TWO = "myIntt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
noPicker.setMinValue(0);
noPicker.setMaxValue(20);
noPicker.setWrapSelectorWheel(true);
noPicker = (NumberPicker) findViewById(R.id.pickerOne);
niPicker.setMinValue(0);
niPicker.setMaxValue(20);
niPicker.setWrapSelectorWheel(true);
niPicker = (NumberPicker) findViewById(R.id.pickerTwo);
}
/**
* Displays the given score for Player1.
*/
public void displayForPlayerOne(int score) {
TextView scoreView = (TextView) findViewById(R.id.playerOneScore);
scoreView.setText(String.valueOf(score));
}
public void addToPlayerOne(View v) {
currentValue = noPicker.getValue();
scorePlayerOne = scorePlayerOne + currentValue;
displayForPlayerOne(scorePlayerOne);
}
public void multiplyTwoPlayerOne(View v) {
currentValue = noPicker.getValue();
scorePlayerOne = scorePlayerOne + currentValue * 2;
displayForPlayerOne(scorePlayerOne);
}
public void multiplyThreePlayerOne(View v) {
currentValue = noPicker.getValue();
scorePlayerOne = scorePlayerOne + currentValue * 3;
displayForPlayerOne(scorePlayerOne);
}
/**
* Displays the given score for Player2.
*/
public void displayForPlayerTwo(int score) {
TextView scoreView = (TextView) findViewById(R.id.playerTwoScore);
scoreView.setText(String.valueOf(score));
}
public void addToPlayerTwo(View v) {
currentValue2 = niPicker.getValue();
scorePlayerTwo = scorePlayerTwo + currentValue2;
displayForPlayerTwo(scorePlayerTwo);
}
public void multiplyTwoPlayerTwo(View v) {
currentValue2 = niPicker.getValue();
scorePlayerTwo = scorePlayerTwo + currentValue2 * 2;
displayForPlayerTwo(scorePlayerTwo);
}
public void multiplyThreePlayerTwo(View v) {
currentValue2 = niPicker.getValue();
scorePlayerTwo = scorePlayerTwo + currentValue2 * 3;
displayForPlayerTwo(scorePlayerTwo);
}
public void reset(View v) {
scorePlayerOne = 0;
scorePlayerTwo = 0;
displayForPlayerTwo(scorePlayerTwo);
displayForPlayerOne(scorePlayerOne);
}
@Override
public void onSaveInstanceState(Bundle outState) {
setContentView(R.layout.activity_main);
super.onSaveInstanceState(outState);
outState.putInt(STATE_SCORE_ONE, scorePlayerOne);
outState.putInt(STATE_SCORE_TWO, scorePlayerTwo);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
setContentView(R.layout.activity_main);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
scorePlayerOne = savedInstanceState.getInt(STATE_SCORE_ONE);
scorePlayerTwo = savedInstanceState.getInt(STATE_SCORE_TWO);
}
}
的xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f5f5f5"
android:orientation="vertical"
android:theme="@android:style/Theme.Material"
tools:context="com.example.android.dartscounter.MainActivity"
tools:ignore="NewApi">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:orientation="vertical">
<TextView
android:id="@+id/player_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="casual"
android:gravity="center_horizontal"
android:padding="16dp"
android:text="Player 1"
android:textColor="#616161"
android:textSize="14sp" />
<TextView
android:id="@+id/playerOneScore"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:fontFamily="casual"
android:gravity="center_horizontal"
android:text="0"
android:textColor="#000000"
android:textSize="56sp" />
<NumberPicker
android:id="@+id/pickerOne"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:clickable="true"
android:descendantFocusability="blocksDescendants"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:soundEffectsEnabled="true"
android:theme="@style/Base.V22.Theme.AppCompat.Light" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="addToPlayerOne"
android:text="add" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="multiplyTwoPlayerOne"
android:text="x 2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="multiplyThreePlayerOne"
android:text="x 3" />
</LinearLayout>
<View
android:layout_width="0.2dp"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:background="#212121"
android:id="@+id/view_view"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="casual"
android:gravity="center_horizontal"
android:padding="16dp"
android:text="Player 2"
android:textColor="#616161"
android:textSize="14sp"
android:id="@+id/player_2"/>
<TextView
android:id="@+id/playerTwoScore"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:fontFamily="casual"
android:gravity="center_horizontal"
android:text="0"
android:textColor="#000000"
android:textSize="56sp" />
<NumberPicker
android:id="@+id/pickerTwo"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:descendantFocusability="blocksDescendants"
android:isScrollContainer="true"
android:theme="@style/Base.V22.Theme.AppCompat.Light" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="addToPlayerTwo"
android:text="add" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="multiplyTwoPlayerTwo"
android:text="x 2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="multiplyThreePlayerTwo"
android:text="x 3" />
</LinearLayout>
</LinearLayout>
<ImageButton
android:id="@+id/imageButton"
style="@android:style/Widget.ImageButton"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_below="@+id/linearLayout"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:onClick="reset"
android:scaleType="centerCrop"
android:visibility="visible"
app:srcCompat="@drawable/vector" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageButton"
android:layout_centerHorizontal="true"
android:text="Reset"
android:textColor="#424242" />
</RelativeLayout>
错误日志:
--------- beginning of crash
03-23 20:55:23.365 2528-2528/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.dartscounter, PID: 2528
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.dartscounter/com.example.android.dartscounter.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.android.dartscounter.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.android.dartscounter-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.android.dartscounter-1/lib/x86, /vendor/lib, /system/lib]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.android.dartscounter.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.android.dartscounter-1/base.apk"],nativeLibraryDirectories=[/data/app/com.example.android.dartscounter-1/lib/x86, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Suppressed: java.lang.ClassNotFoundException: com.example.android.dartscounter.MainActivity
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 12 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
03-23 20:55:29.709 2528-2528/com.example.android.dartscounter I/Process: Sending signal. PID: 2528 SIG: 9
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.dartscounter" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
ClassNotFoundException
通常是因为未在Android Manifest中声明活动而导致的。使用以下代码将活动添加到清单:
<activity android:name="YourActivityName"/>
答案 1 :(得分:0)
我实际上找到了一种保存我的活动状态的方法,它可以正常运行。
下面提供的最终工作Java代码:
<__main__.Node instance at 0x108426098>
[<__main__.Node instance at 0x108426128>, <__main__.Node instance at 0x108426170>, <__main__.Node instance at 0x1084261b8>]
[<__main__.Node instance at 0x108426200>, <__main__.Node instance at 0x108426248>, <__main__.Node instance at 0x108426290>]
[<__main__.Node instance at 0x1084262d8>]