请帮我找出代码的问题。首先,应用程序按预期工作,但当屏幕方向从纵向更改为横向时,应用程序正在破坏,这就是为什么我决定添加“onSaveInstanceState(Bundle outState)”和“onRestoreInstanceState(Bundle savedInstanceState)”的原因,我在Java中是个新手,但我试着自己解决了四天,没有任何实际结果。
以下是我的Java代码和日志:
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>
ClassNotFoundException
通常是由于活动未在Android清单中声明而导致的。使用以下代码将活动添加到清单:
<activity android:name="YourActivityName"/>
问题内容: 我写了一个Qt应用程序,它将在Linux上运行。它应该在启动时运行。 它应该在每个Linux- Suse,RedHat等上运行。 我应该写什么脚本,放在哪里? 我根本不知道如何编写脚本,因此,如果您附上示例,我将不胜感激。 问题答案: 您需要为您的应用程序创建一个桌面入口文件(请参阅此处),并将其放置在用户目录中。 Window Manager启动时,该目录中的任何桌面条目文件都将被执
我在运行android应用程序时遇到这个错误 05-25 23:58:44.251:E/AndroidRuntime(1128):java.lang.RuntimeException:无法启动activity ComponentInfo{info.AndroidHive.TabsSwipe/Info.AndroidHive.TabsSwipe.MainActivity}:Android.View.
问题内容: 我有一个包含Java应用程序的JAR文件。如何使它从Windows开始而不需要用户交互? 问题答案: 创建一个.bat文件,并将其放入其中: 然后将.bat文件放入Windows启动文件夹。 还有一件事:使用java和javaw之间是有区别的。虽然在调试应用程序时使用Java更好,但应用程序会打印文本或类似内容,而在不需要时,javaw则更好。为什么?因为java使用控制台显示所有应用
我有一个包含Java应用程序的JAR文件。如何在不需要用户交互的情况下从Windows开始?
当我按下按钮时,应用程序会崩溃。每个转换“国名”功能都是一个按钮。我会在评论中发布错误。提前道谢。