我一直在尝试为Android创建一个随机数生成器,但我似乎无法让应用程序在虚拟设备上运行而不会崩溃。
以下是错误:
07-01 11:37:00.768: D/AndroidRuntime(2022): Shutting down VM
07-01 11:37:00.808: W/dalvikvm(2022): threadid=1: thread exiting with uncaught exception (group=0xb0d5cb20)
07-01 11:37:00.808: E/AndroidRuntime(2022): FATAL EXCEPTION: main
07-01 11:37:00.808: E/AndroidRuntime(2022): Process: com.bruinstech.numbergen, PID: 2022
07-01 11:37:00.808: E/AndroidRuntime(2022): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.bruinstech.numbergen/com.bruinstech.numbergen.MainAcitivity}: java.lang.ClassNotFoundException: Didn't find class "com.bruinstech.numbergen.MainAcitivity" on path: DexPathList[[zip file "/data/app/com.bruinstech.numbergen-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.bruinstech.numbergen-1, /system/lib]]
07-01 11:37:00.808: E/AndroidRuntime(2022): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
07-01 11:37:00.808: E/AndroidRuntime(2022): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-01 11:37:00.808: E/AndroidRuntime(2022): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-01 11:37:00.808: E/AndroidRuntime(2022): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-01 11:37:00.808: E/AndroidRuntime(2022): at android.os.Handler.dispatchMessage(Handler.java:102)
07-01 11:37:00.808: E/AndroidRuntime(2022): at android.os.Looper.loop(Looper.java:136)
07-01 11:37:00.808: E/AndroidRuntime(2022): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-01 11:37:00.808: E/AndroidRuntime(2022): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 11:37:00.808: E/AndroidRuntime(2022): at java.lang.reflect.Method.invoke(Method.java:515)
07-01 11:37:00.808: E/AndroidRuntime(2022): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-01 11:37:00.808: E/AndroidRuntime(2022): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-01 11:37:00.808: E/AndroidRuntime(2022): at dalvik.system.NativeStart.main(Native Method)
07-01 11:37:00.808: E/AndroidRuntime(2022): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.bruinstech.numbergen.MainAcitivity" on path: DexPathList[[zip file "/data/app/com.bruinstech.numbergen-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.bruinstech.numbergen-1, /system/lib]]
07-01 11:37:00.808: E/AndroidRuntime(2022): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
07-01 11:37:00.808: E/AndroidRuntime(2022): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
07-01 11:37:00.808: E/AndroidRuntime(2022): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
07-01 11:37:00.808: E/AndroidRuntime(2022): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
07-01 11:37:00.808: E/AndroidRuntime(2022): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
07-01 11:37:00.808: E/AndroidRuntime(2022): ... 11 more
我的 MainActivity.java: 包com.bruinstech.numbergen;
import java.util.Random;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText nu1;
EditText nu2;
TextView number;
Button button1;
Random randomGenerator = new Random();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v){
int y = Integer.parseInt(nu1.getText().toString());
int x = Integer.parseInt(nu2.getText().toString());
number.setText("Random Number: " + randomGenerator(x, y));
}
});
AdView adView = (AdView)this.findViewById(R.id.adView1);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
public static int randomGenerator(int y, int x) {
Random rand = new Random();
int randomNum = rand.nextInt((y - x) + 1) + y;
return randomNum;
}
}
我的 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.android.gms.ads"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp"
android:ems="10"
android:hint="@string/value"
android:inputType="number" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp"
android:ems="10"
android:hint="@string/value"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/nu2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_alignRight="@+id/editText2"
android:layout_below="@+id/editText1"
android:layout_marginTop="18dp"
android:text="@string/no2"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="center" />
<TextView
android:id="@+id/nu1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/editText1"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="@string/no1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_alignRight="@+id/editText2"
android:layout_centerVertical="true"
android:text="@string/but" />
<TextView
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignLeft="@+id/button1"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="16dp" />
<com.google.android.gms.ads.AdView
android:id="@+id/adView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adUnitId="@string/ad_unit_id"
ads:adSize="BANNER"/>
</RelativeLayout>
任何帮助都会得到极大的评价
答案 0 :(得分:1)
问题出在AndroidManifest.xml
。检查您在那里提供的MainActivity的路径,更正它并解决您的问题。
答案 1 :(得分:1)
您的AndroidManifest.xml文件中有拼写错误。
com.bruinstech.numbergen.MainAcitivity
应该是com.bruinstech.numbergen.MainActivity
(通常,它必须与您的班级名称相匹配)。