2

Android應用程序在啓動時不斷崩潰。 儘管如此,一切看起來都很好。Android應用程序在啓動時崩潰

堆棧跟蹤:

01-24 21:23:41.417: I/Process(767): Sending signal. PID: 767 SIG: 9 
01-24 21:41:36.316: W/Trace(890): Unexpected value from nativeGetEnabledTags: 0 
01-24 21:41:36.485: W/Trace(890): Unexpected value from nativeGetEnabledTags: 0 
01-24 21:41:36.515: W/Trace(890): Unexpected value from nativeGetEnabledTags: 0 
01-24 21:41:36.515: D/dalvikvm(890): newInstance failed: p0 i0 [0 a1 
01-24 21:41:36.515: D/AndroidRuntime(890): Shutting down VM 
01-24 21:41:36.515: W/dalvikvm(890): threadid=1: thread exiting with uncaught  exception (group=0x40a70930) 
01-24 21:41:36.635: E/AndroidRuntime(890): FATAL EXCEPTION: main 
01-24 21:41:36.635: E/AndroidRuntime(890): java.lang.RuntimeException: Unable to  instantiate activity ComponentInfo{com.example.bmisummative/com.example.bmisummative.MainActivity}:  java.lang.InstantiationException: can't instantiate class  com.example.bmisummative.MainActivity 
01-24 21:41:36.635: E/AndroidRuntime(890): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106) 
01-24 21:41:36.635: E/AndroidRuntime(890): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
01-24 21:41:36.635: E/AndroidRuntime(890): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
01-24 21:41:36.635: E/AndroidRuntime(890): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
01-24 21:41:36.635: E/AndroidRuntime(890): at android.os.Handler.dispatchMessage(Handler.java:99) 
01-24 21:41:36.635: E/AndroidRuntime(890): at android.os.Looper.loop(Looper.java:137) 
01-24 21:41:36.635: E/AndroidRuntime(890): at android.app.ActivityThread.main(ActivityThread.java:5039) 
01-24 21:41:36.635: E/AndroidRuntime(890): at java.lang.reflect.Method.invokeNative(Native Method) 
01-24 21:41:36.635: E/AndroidRuntime(890): at java.lang.reflect.Method.invoke(Method.java:511) 
01-24 21:41:36.635: E/AndroidRuntime(890): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
01-24 21:41:36.635: E/AndroidRuntime(890): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
01-24 21:41:36.635: E/AndroidRuntime(890): at dalvik.system.NativeStart.main(Native Method) 
01-24 21:41:36.635: E/AndroidRuntime(890): Caused by: java.lang.InstantiationException: can't instantiate class com.example.bmisummative.MainActivity 
01-24 21:41:36.635: E/AndroidRuntime(890): at java.lang.Class.newInstanceImpl(Native Method) 
01-24 21:41:36.635: E/AndroidRuntime(890): at java.lang.Class.newInstance(Class.java:1319) 
01-24 21:41:36.635: E/AndroidRuntime(890): at android.app.Instrumentation.newActivity(Instrumentation.java:1054) 
01-24 21:41:36.635: E/AndroidRuntime(890): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097) 
01-24 21:41:36.635: E/AndroidRuntime(890): ... 11 more 
01-24 21:46:36.745: I/Process(890): Sending signal. PID: 890 SIG: 9 

MainActivity類:

package com.example.bmisummative; 

import android.app.Activity; 
import android.content.DialogInterface; 
import android.view.View.OnClickListener; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public abstract class MainActivity extends Activity implements OnClickListener { 
/** Called when the activity is first created. */ 
Button btn,btn1,btn2; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    btn = (Button) findViewById(R.id.buttonBMI); 
    btn.setOnClickListener(this); 
    btn1 = (Button) findViewById(R.id.buttonGain); 
    btn1.setOnClickListener(this); 
    btn2 = (Button) findViewById(R.id.buttonLoss); 
    btn2.setOnClickListener(this); 

} 

@Override 
public void onClick(View v) { 
    if (v.getId() == R.id.buttonBMI) { 
    startActivity(new Intent(this, MainActivity.class)); 
    } 
    if (v.getId() == R.id.buttonGain) { 
    startActivity(new Intent(this, weightgain.class)); 
    } 
    if (v.getId() == R.id.buttonLoss) { 
     startActivity(new Intent(this, weightgain.class)); 
     } 

} 

} 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    tools:context=".StartingPoint" 
    android:background="@drawable/grunge"> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Main Menu" 
     android:textSize="45dp" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:textColor="#FFFFFF" 
     android:id="@+id/tvDisplay"/> 

    <Button 
     android:id="@+id/buttonBMI" 
     android:layout_width="250dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="BMI Calculator" 
     android:textSize="20dp" /> 

    <Button 
     android:id="@+id/buttonLoss" 
     android:layout_width="250dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="Weight Loss Workouts" 
     android:textSize="20dp" /> 

    <Button 
     android:id="@+id/buttonGain" 
     android:layout_width="250dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="Muscle Gain Workouts" 
     android:textSize="20dp" /> 

</LinearLayout> 

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.bmisummative" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

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

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.example.bmiworking.calculator" 
      android:label="@string/app_name" > 
      </activity> 

      <activity 
      android:name="com.example.bmiworking.weightgain" 
      android:label="@string/app_name" > 
      </activity> 
      <activity 
      android:name="com.example.bmiworking.weightloss" 
      android:label="@string/app_name" > 
      </activity> 
      </application> 
</manifest> 

回答

3

當你在清單中聲明你的活動,嘗試活動名稱之前刪除軟件包,讓你獲得android:name=".MainActivity"和做與其他活動一樣。

好吧,我的第一個答案是錯的。你的應用程序崩潰的原因是因爲你已經聲明你的主要活動是抽象刪除它,你的應用程序應該運行。

+0

仍然崩潰。 – suggamuggafugga

+1

完全合格是好的。活動是抽象的。 –

+0

@ErikNedwidek在你發佈之前編輯;) – jcw

1

你的活動不應該是抽象的。

您也應該習慣在至少

try { 
    /// Do some work 
} catch (Throwable t) { 
    Log.e(TAG, t.getMessage(), t); 
} 

在我的活動類包裝您的功能,我定義標記爲:

public static final String TAG = "NameMeSomethingUnique"; 

改變字符串的值一般名稱活動。

編輯

而不是顯示整個班級改寫。我只是修改try/catch的onCreate,並將TAG常量添加爲類成員。您可以用相同的方法將try/catch塊添加到其他方法。

public class MainActivity extends Activity implements OnClickListener { // REMOVED abstract keyword - EJN 
/** Called when the activity is first created. */ 
Button btn,btn1,btn2; 
private static final String TAG = "MainActivity"; // Used to make logging easier - EJN 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    try { // Wrapped to trap any possible exceptions so we don't just crash - EJN 
     setContentView(R.layout.activity_main); 
     btn = (Button) findViewById(R.id.buttonBMI); 
     btn.setOnClickListener(this); 
     btn1 = (Button) findViewById(R.id.buttonGain); 
     btn1.setOnClickListener(this); 
     btn2 = (Button) findViewById(R.id.buttonLoss); 
     btn2.setOnClickListener(this); 
    } catch (Throwable t) { 
     Log.e(TAG, t.getMessage(), t); 
    } 
} 
+0

你可以重寫我的代碼作爲一個例子,我仍然沒有得到它。 – suggamuggafugga