2016-05-30 31 views
1

嗨,我很新的android。我認爲第一個應用的好主意是計算器。我創建了該應用程序完全安裝並編譯正確。但是當我在設備上運行它時,它強制關閉。我認爲在我的AndroidManifest.xml文件中可能存在問題,但我不知道它是什麼。AndroidManifes xml的問題。 App部隊在運行中關閉

這是可以將Java代碼:

import android.os.Bundle; 
import android.app.Activity; 
import android.text.Editable; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

import com.awesomeapps.misael.simplecalculator.R; 

public class MainActivity extends Activity implements View.OnClickListener{ 
    Button one, two, three, four, five, six, seven, eight, nine, zero, add, sub, mul, div, cancel, equal; 
    EditText disp; 
    int op1; 
    int op2; 
    String optr; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.content_main); 

     one = (Button) findViewById(R.id.one); 
     two = (Button) findViewById(R.id.two); 
     three = (Button) findViewById(R.id.three); 
     four = (Button) findViewById(R.id.four); 
     five = (Button) findViewById(R.id.five); 
     six = (Button) findViewById(R.id.six); 
     seven = (Button) findViewById(R.id.seven); 
     eight = (Button) findViewById(R.id.eight); 
     nine = (Button) findViewById(R.id.nine); 
     zero = (Button) findViewById(R.id.zero); 
     add = (Button) findViewById(R.id.add); 
     sub = (Button) findViewById(R.id.sub); 
     mul = (Button) findViewById(R.id.mul); 
     div = (Button) findViewById(R.id.div); 
     cancel = (Button) findViewById(R.id.cancel); 
     equal = (Button) findViewById(R.id.equal); 

     disp = (EditText) findViewById(R.id.editText); 

     try { 
      one.setOnClickListener(this); 
      two.setOnClickListener(this); 
      three.setOnClickListener(this); 
      four.setOnClickListener(this); 
      five.setOnClickListener(this); 
      six.setOnClickListener(this); 
      seven.setOnClickListener(this); 
      eight.setOnClickListener(this); 
      nine.setOnClickListener(this); 
      zero.setOnClickListener(this); 
      cancel.setOnClickListener(this); 
      add.setOnClickListener(this); 
      sub.setOnClickListener(this); 
      mul.setOnClickListener(this); 
      div.setOnClickListener(this); 
      equal.setOnClickListener(this); 
     } catch (Exception e) { 
     } 
    } 

    public void operation() { 
     if (optr.equals("+")) { 
      op2 = Integer.parseInt(disp.getText().toString()); 
      disp.setText(""); 
      op1 = op1 + op2; 
      disp.setText("Result: " + Integer.toString(op1)); 
     } else if (optr.equals("-")) { 
      op2 = Integer.parseInt(disp.getText().toString()); 
      disp.setText(""); 
      op1 = op1 - op2; 
      disp.setText("Result" + Integer.toString(op1)); 
     } else if (optr.equals("*")) { 
      op2 = Integer.parseInt(disp.getText().toString()); 
      disp.setText(""); 
      op1 = op1 * op2; 
      disp.setText("Result" + Integer.toString(op1)); 
     } else if (optr.equals("/")) { 
      op2 = Integer.parseInt(disp.getText().toString()); 
      disp.setText(""); 
      op1 = op1/op2; 
      disp.setText("Result" + Integer.toString(op1)); 
     } 
    } 

    @Override 
    public void onClick(View arg0) { 
     Editable str = disp.getText(); 
     switch (arg0.getId()) { 
      case R.id.one: 
       if (op2 != 0) { 
        op2 = 0; 
        disp.setText(""); 
       } 
       str = str.append(two.getText()); 
       disp.setText(str); 
       break; 
      case R.id.two: 
       if (op2 != 0) { 
        op2 = 0; 
       } 
       str = str.append(two.getText()); 
       disp.setText(str); 
      case R.id.three: 
       if (op2 != 0) { 
        op2 = 0; 
       } 
       str = str.append(three.getText()); 
       disp.setText(str); 
       break; 
      case R.id.four: 
       if (op2 != 0) { 
        op2 = 0; 
       } 
       str = str.append(four.getText()); 
       disp.setText(str); 
       break; 
      case R.id.five: 
       if (op2 != 0) { 
        op2 = 0; 
       } 
       str = str.append(five.getText()); 
       disp.setText(str); 
       break; 
      case R.id.six: 
       if (op2 != 0) { 
        op2 = 0; 
       } 
       str = str.append(six.getText()); 
       disp.setText(str); 
       break; 
      case R.id.seven: 
       if (op2 != 0) { 
        op2 = 0; 
       } 
       str = str.append(eight.getText()); 
       disp.setText(str); 
       break; 
      case R.id.eight: 
       if (op2 != 0) { 
        op2 = 0; 
       } 
       str = str.append(nine.getText()); 
       disp.setText(str); 
       break; 
      case R.id.nine: 
       if (op2 != 0) { 
        op2 = 0; 
       } 
       str = str.append(zero.getText()); 
       disp.setText(str); 
       break; 
      case R.id.cancel: 
       op1 = 0; 
       op2 = 0; 
       disp.setText(""); 
       disp.setHint("Perform Operation"); 
       break; 
      case R.id.add: 
       optr = "+"; 
       if (op1 == 0) { 
        op1 = Integer.parseInt(disp.getText().toString()); 
        disp.setText(""); 
       } else if (op2 != 0) { 
        op2 = 0; 
        disp.setText(""); 
       } else { 
        op2 = Integer.parseInt(disp.getText().toString()); 
        disp.setText(""); 
        op1 = op1 + op2; 
        disp.setText("Result" + Integer.toString(op1)); 
       } 
       break; 
      case R.id.sub: 
       optr = "-"; 
       if (op1 == 0) { 
        op1 = Integer.parseInt(disp.getText().toString()); 
        disp.setText(""); 
       } else if (op2 != 0) { 
        op2 = 0; 
        disp.setText(""); 
       } else { 
        op2 = Integer.parseInt(disp.getText().toString()); 
        disp.setText(""); 
        op1 = op1 - op2; 
        disp.setText("Result" + Integer.toString(op1)); 
       } 
       break; 
      case R.id.mul: 
       optr = "*"; 
       if (op1 == 0) { 
        op1 = Integer.parseInt(disp.getText().toString()); 
        disp.setText(""); 
       } else if (op2 != 0) { 
        op2 = 0; 
        disp.setText(""); 
       } else { 
        op2 = Integer.parseInt(disp.getText().toString()); 
        disp.setText(""); 
        op1 = op1 * op2; 
        disp.setText("Result" + Integer.toString(op1)); 
       } 
       break; 
      case R.id.div: 
       optr = "/"; 
       if (op1 == 0) { 
        op1 = Integer.parseInt(disp.getText().toString()); 
        disp.setText(""); 
       } else if (op2 != 0) { 
        op2 = 0; 
        disp.setText(""); 
       } else { 
        op2 = Integer.parseInt(disp.getText().toString()); 
        disp.setText(""); 
        op1 = op1/op2; 
        disp.setText("Result" + Integer.toString(op1)); 
       } 
       break; 
      case R.id.equal: 
       if (optr.equals(null)) { 
        if (op2 != 0) { 
         if (optr.equals("+")) { 
          disp.setText(""); 
        /*op1 = op1+op2;*/ 
          disp.setText("Result:" + Integer.toString(op1)); 
         } else if (optr.equals("-")) { 
          disp.setText("");/* 
        op1 = op1 - op2;*/ 
          disp.setText("Result" + Integer.toString(op1)); 
         } else if (optr.equals("*")) { 
          disp.setText("");/* 
        op1 = op1*op2;*/ 
          disp.setText("Result" + Integer.toString(op1)); 
         } else if (optr.equals("/")) { 
          disp.setText("");/* 
        op1=op1/op2;*/ 
          disp.setText("Result" + Integer.toString(op1)); 
         } 
        } else { 
         operation(); 
        } 
       } 
       break; 
     } 
    } 
} 

這是我AndroidManifest XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.awesomeapps.misael.simplecalculator"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

哦,我也收到此錯誤信息:

05-29 21:48:03.091 10728-10728/com.awesomeapps.misael.simplecalculator E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.awesomeapps.misael.simplecalculator, PID: 10728 
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.awesomeapps.misael.simplecalculator/com.awesomeapps.misael.simplecalculator.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.awesomeapps.misael.simplecalculator.MainActivity" on path: DexPathList[[zip file "/data/app/com.awesomeapps.misael.simplecalculator-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]] 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2641) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2855) 
at android.app.ActivityThread.access$900(ActivityThread.java:181) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:6117) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.awesomeapps.misael.simplecalculator.MainActivity" on path: DexPathList[[zip file "/data/app/com.awesomeapps.misael.simplecalculator-1/base.apk"],nativeLibraryDirectories=[/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:1079) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2631) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2855) 
at android.app.ActivityThread.access$900(ActivityThread.java:181) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:6117) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
Suppressed: java.lang.ClassNotFoundException: com.awesomeapps.misael.simplecalculator.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) 
... 13 more 
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available 

如果任何人都可以請通過告訴我要在我的Java代碼或Android清單xml文件中修復哪些內容來提供幫助。或者,也許它完全不同。

任何幫助將是真棒,非常感謝!謝謝! :)

+2

在你的activity類中沒有'package com.awesomeapps.misael.simplecalculator;'。 – Ufkoku

+0

具有'MainActivity'類的包的名稱是什麼?我想你把不正確的包裹。 –

+1

添加軟件包並刪除'''import com.awesomeapps.misael.simplecalculator.R;''' –

回答

-2

如果要啓用在您的項目多DEX然後只要進入gradle.builder

,並在您dependencie添加此

dependencies { 
     compile 'com.android.support:multidex:1.0.0'} 

,那麼你必須添加

defaultConfig { 
    ... 
    minSdkVersion 14 
    targetSdkVersion 21 
    ... 

    // Enabling multidex support. 
     multiDexEnabled true} 

然後打開一個類並將其擴展到應用程序如果您的應用程序使用擴展了Application類,則可以重寫oncrete()方法並調用。

 //add this code into your application class 
public class MyApplication extends Application { 
     // public Context context; 
@Override 
public void onCreate() { 
    super.onCreate(); 
    } 
} 
@Override 
protected void attachBaseContext(Context newBase) { 
    super.attachBaseContext(newBase); 
    MultiDex.install(this); 
} 

啓用multidex。

最後加入到你的清單

<?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.android.multidex.myapplication"> 
    <application 
    ... 
    android:name=".MyApplication""> 
    ... 
    </application> 
    </manifest> 
+0

我也得到相同的錯誤(classNotFoundException),添加這段代碼,很好 – RejoylinLokeshwaran

+0

我正在使用Android工作室我不知道該怎麼去做更改 – AmateurProgrammer

0

添加下面一行在你MainActivity類的頂部:

package com.awesomeapps.misael.simplecalculator; 

在此之後的清潔和重建項目。

0

我試圖運行你的代碼並將所有東西都粘貼到新項目中。我所做的未來是:

1)刪除了此

import com.awesomeapps.misael.simplecalculator.R; 

2)進口我的包MainActivity.java(這是你的情況我想的第一行)

package com.awesomeapps.misael.simplecalculator; 

我沒有碰過清單文件。然後它運行沒有任何錯誤。

但是,我應該提到在你的代碼中有很多錯誤。至少,它沒有顯示結果,當您按下某些數字按鈕時,它們會輸入錯誤的值。運行代碼並修復錯誤。最後,如果你在你的佈局中包含了一些textview,這將會很好,這將解釋如何使用你的計算器。快樂編碼:)

+0

我嘗試了你所做的,並且表示oacke R不存在。 – AmateurProgrammer

+0

@AmateurProgrammer您是否在頂部包含了包名?它應該在MainActivity.java的第一行 – Marat

+0

我的包行中有一個巨大的錯字,並且在XML文件中的活動名稱行中有錯誤,但是我終於完成了它的工作!現在我知道我有很多錯誤來解決大聲笑感謝您的幫助! :D – AmateurProgrammer

0

加包名在MainActivity

package com.awesomeapps.misael.simplecalculator; 

刪除此行import com.awesomeapps.misael.simplecalculator.R;

刪除build文件夾 清潔和重建項目。希望它能解決你的問題。