2017-03-27 195 views
-4

我剛剛在學校開始了一個android開發類,想知道爲什麼我的代碼不工作?任何幫助將不勝感激。我正在創建一個提示跟蹤器應用程序,以幫助我在提供比薩餅時跟蹤我的提示。我知道創建了一個類似的應用程序,但我想創建自己的應用程序,這也是我本學期的最終項目。現在,我只是試圖在窗口之間導航,以確保我可以將所有內容鏈接在一起,然後才能在UI上發瘋。我沒有看到實際存在問題的位置,但當我點擊主屏幕上的「傳送」按鈕時,出現錯誤「不幸的是,[app]已停止工作」。所以我的第二個窗口從未真正出現。另外,我的應用程序有時非常緩慢且無響應。感謝任何能夠幫助的人。我的代碼如下:Android應用程序。應用程序工作非常緩慢,並導致錯誤

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dip" 
    android:layout_weight="1" 
    android:gravity="center|bottom" 
    android:orientation="vertical"> 

    <Button 
     android:layout_width="155dp" 
     android:layout_height="60dp" 
     android:layout_gravity="center_horizontal|center" 
     android:layout_marginBottom="10dp" 
     android:background="@drawable/button_bg" 
     android:text="Delivery" 
     android:id="@+id/deliveryButton" /> 

    <Button 
     android:layout_width="155dp" 
     android:layout_height="60dp" 
     android:layout_gravity="center_horizontal|center" 
     android:layout_marginBottom="10dp" 
     android:background="@drawable/button_bg" 
     android:text="LOG" 
     android:id="@+id/logButton" /> 

    <Button 
     android:layout_width="155dp" 
     android:layout_height="60dp" 
     android:layout_gravity="center_horizontal|center" 
     android:layout_marginBottom="10dp" 
     android:background="@drawable/button_bg" 
     android:text="MAPS" 
     android:id="@+id/mapsButton" /> 

我主要的Java文件的代碼是:

public class MainActivity extends AppCompatActivity { 

private Button launchDeliveryActivity; 

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

    launchDeliveryActivity = (Button) findViewById(R.id.deliveryButton); 

    launchDeliveryActivity.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View view) 
     { 
      launchActivity(); 
     } 
    }); 
} 

private void launchActivity() 
{ 
    Intent intent = new Intent(this, orderDetailsActivity.class); 
    startActivity(intent); 
} 

我的第二個窗口是我趕緊扔在一起,以確保我將能夠從導航主屏幕,第二個窗口和背部:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/order_details" 
    android:orientation="vertical"> 

    <Button 
     android:layout_width="120dp" 
     android:layout_height="60dp" 
     android:text="Home" 
     android:id="@+id/homeScreenButton" /> 
</LinearLayout> 

最後,我的第二個Java文件:

public class orderDetailsActivity extends AppCompatActivity { 

private Button homeScreen; 

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

    homeScreen = (Button) findViewById(R.id.homeScreenButton); 

    homeScreen.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View view) 
     { 
      finish(); 
     } 
    }); 
} 

日誌是:

上午01時29分執行的任務:[:應用:assembleDebug]

上午01時29分搖籃建設完成了1秒781ms

上午01點31執行任務: :應用:assembleDebug]

上午01時31分在搖籃7S 15ms的

上午01時31分建立成品即時運行進行全方位構建和安裝,因爲在設備上的安裝不符合ŧ他本地建立在磁盤上。 (不要再顯示)

上午01點40執行任務:[:應用:assembleDebug]

上午01時40搖籃籌建完成了1秒203ms

上午01點43執行任務: :應用程序:assembleDebug]

上午01點43分搖籃籌建完成了在983ms

編輯: 的logcat的是:

03-27 03:37:59.550 31525-31525/com.example.boley.personaldeliveryassistant E/AndroidRuntime: FATAL EXCEPTION: main 
                         Process: com.example.boley.personaldeliveryassistant, PID: 31525 
                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.boley.personaldeliveryassistant/com.example.boley.personaldeliveryassistant.orderDetailsActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown> 
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253) 
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
                          at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                          at android.os.Looper.loop(Looper.java:158) 
                          at android.app.ActivityThread.main(ActivityThread.java:7224) 
                          at java.lang.reflect.Method.invoke(Native Method) 
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
                          Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown> 
                          at android.view.LayoutInflater.inflate(LayoutInflater.java:551) 
                          at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
                          at android.view.LayoutInflater.inflate(LayoutInflater.java:380) 
                          at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
                          at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                          at com.example.boley.personaldeliveryassistant.orderDetailsActivity.onCreate(orderDetailsActivity.java:15) 
                          at android.app.Activity.performCreate(Activity.java:6876) 
                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135) 
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206) 
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)  
                          at android.app.ActivityThread.access$1100(ActivityThread.java:221)  
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)  
                          at android.os.Handler.dispatchMessage(Handler.java:102)  
                          at android.os.Looper.loop(Looper.java:158)  
                          at android.app.ActivityThread.main(ActivityThread.java:7224)  
                          at java.lang.reflect.Method.invoke(Native Method)  
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)  
                          Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown> 
                          at android.view.LayoutInflater.createView(LayoutInflater.java:657) 
                          at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
                          at android.view.LayoutInflater.onCreateView(LayoutInflater.java:706) 
                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:774) 
                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716) 
                          at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 
                          at android.view.LayoutInflater.inflate(LayoutInflater.java:429)  
                          at android.view.LayoutInflater.inflate(LayoutInflater.java:380)  
                          at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)  
                          at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)  
                          at com.example.boley.personaldeliveryassistant.orderDetailsActivity.onCreate(orderDetailsActivity.java:15)  
                          at android.app.Activity.performCreate(Activity.java:6876)  
                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)  
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)  
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)  
                          at android.app.ActivityThread.access$1100(ActivityThread.java:221)  
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)  
                          at android.os.Handler.dispatchMessage(Handler.java:102)  
                          at android.os.Looper.loop(Looper.java:158)  
                          at android.app.ActivityThread.main(ActivityThread.java:7224)  
                          at java.lang.reflect.Method.invoke(Native Method)  
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)  
                          Caused by: java.lang.reflect.InvocationTargetException 
                          at java.lang.reflect.Constructor.newInstance(Native Method) 
                          at android.view.LayoutInflater.createView(LayoutInflater.java:631) 
                          at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)  
                          at android.view.LayoutInflater.onCreateView(LayoutInflater.java:706)  
                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:774)  
                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)  
                          at android.view.LayoutInflater.inflate(LayoutInflater.java:498)  
                          at android.view.LayoutInflater.inflate(LayoutInflater.java:429)  
                          at android.view.LayoutInflater.inflate(LayoutInflater.java:380)  
                          at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)  
                          at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)  
                          at com.example.boley.personaldeliveryassistant.orderDetailsActivity.onCreate(orderDetailsActivity.java:15)  
                          at android.app.Activity.performCreate(Activity.java:6876)  
                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)  
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)  
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)  
                          at android.app.ActivityThread.access$1100(ActivityThread.java:221)  
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)  
                          at android.os.Handler.dispatchMessage(Handler.java:102)  
                          at android.os.Looper.loop(Looper.java:158)  
                          at android.app.ActivityThread.main(ActivityThread.java:7224)  
                          at java.lang.reflect.Method.invoke(Native Method)  
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)  
                          Caused by: java.lang.OutOfMemoryError: Failed to allocate a 207355916 byte allocation with 16773008 free bytes and 53MB until OOM 
                          at dalvik.system.VMRuntime.newNonMovableArray(Native Method) 
                          at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
                          at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856) 
                          at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675) 
                          at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:2228) 
                          at android.content.res.Resources.loadDrawableForCookie(Resources.java:4215) 
                          at android.content.res.Resources.loadDrawable(Resources.java:4089) 
                          at android.content.res.Resources.loadDrawable(Resources.java:3939) 
                          at android.content.res.TypedArray.getDrawable(TypedArray.java:886) 
                          at android.view.View.<init>(View.java:4230) 
                          at android.view.ViewGroup.<init>(ViewGroup.java:589) 
                          at android.widget.LinearLayout.<init>(LinearLayout.java:202) 
                          at android.widget.LinearLayout.<init>(LinearLayout.java:198) 
                          at android.widget.LinearLayout.<init>(LinearLayout.java:194) 
                          at java.lang.reflect.Constructor.newInstance(Native Method)  
                          at android.view.LayoutInflater.createView(LayoutInflater.java:631)  
                          at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)  
                          at android.view.LayoutInflater.onCreateView(LayoutInflater.java:706)  
                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:774)  
                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)  
                          at android.view.LayoutInflater.inflate(LayoutInflater.java:498)  
                          at android.view.LayoutInflater.inflate(LayoutInflater.java:429)  
                          at android.view.LayoutInflater.inflate(LayoutInflater.java:380)  
                          at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)  
                          at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)  
                          at com.example.boley.personaldeliveryassistant.orderDetailsActivity.onCreate(orderDetailsActivity.java:15)  
                          at android.app.Activity.performCreate(Activity.java:6876)  
                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)  
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)  
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)  
                          at android.app.ActivityThread.access$1100(ActivityThread.java:221)  
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)  
                          at android.os.Handler.dispatchMessage(Handler.java:102)  
                          at android.os.Looper.loop(Looper.java:158)  
                          at android.app.ActivityThread.main(ActivityThread.java:7224)  
                          at java.lang.reflect.Method.invoke(Native Method)  
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)   
+1

請加日誌 –

+0

請加錯! – BinaryMan

+0

只有當我在手機或模擬器上運行應用程序時,我纔會在Android Studio中收到任何錯誤。該應用加載非常緩慢,當我點擊「傳遞」按鈕,這是按鈕,帶我到第二個窗口,它說:「不幸的是,個人傳遞助理已停止。」第二個窗口從不顯示。 –

回答

0

ANR的可疑是可拉伸「ORDER_DETAILS」。由於第二個活動中沒有其他組件。請檢查可繪製的「order_details」的大小。如果它很大,那麼這可能是ANR的原因。嘗試通過更改drawable或刪除它。

請檢查設備的日誌,便有「I_am ANR」標籤。 anr的日誌中可能有一些信息。

+0

顯然,我用於背景的兩張圖片都太大了。我接受了你的建議並從我的xml文件中刪除了drawables,導致窗口只是一個帶有按鈕的純灰色。現在它完美地在窗戶之間進行導航。謝謝你的快速反應!它只是糟透了,我花了很多時間在Photoshop上做這些。我沒有改變圖像的大小,因爲我希望它可以根據不同的屏幕尺寸進行縮放,因此使圖像比我需要的大。如果我裁剪或調整大小,是否還有辦法使用這些圖像? –

+0

是的,您可以通過裁剪或調整大小來縮小圖像的大小來使用它們。儘量保持圖像的大小以KB爲單位。如果您想根據設備的大小進行縮放,可以通過編程方式完成。檢查下面的鏈接:https://developer.android.com/topic/performance/graphics/load-bitmap.html – Sonam

0

我認爲錯誤越來越引起:java.lang.OutOfMemoryError:無法分配與16773008個免費字節一個字節207355916分配和53MB顯示日誌,以便首先從ORDER_DETAILS LinearLayout中刪除圖像和檢查,如果你想使用這個圖片然後減小尺寸,並設置運行時check this

+0

Android Studio似乎已自動爲我完成。 –