2014-07-01 45 views
0

我一直在試圖爲android創建一個隨機數生成器,但我似乎無法讓應用程序在虛擬設備上運行而不會崩潰。致命的異常:主要的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> 

任何幫助將大大appraicated

+0

請張貼您的清單.. – Lal

回答

1

您的AndroidManifest.xml文件中有錯字。

com.bruinstech.numbergen.MainAcitivity應改爲com.bruinstech.numbergen.MainActivity(通常,它必須與您的班級名稱相匹配)。

+0

嗯,我覺得自己像一個白癡......感謝您的快速回復 – bruins1993

+0

@ bruins1993別擔心,我們都遇到過這樣的問題;) – matiash

1

的問題是在你的AndroidManifest.xml。檢查你在那裏提供的MainActivity的路徑,糾正它,你的問題應該得到解決。