2011-11-17 51 views
0

因此,我正在編寫一個應用程序,出於某種原因,當我嘗試獲取FrameLayout的高度時,出現運行時異常。在整個設計過程中我都會使用重量,所以我想仔細檢查他們是我需要的高度。訪問FrameLayout時獲取運行時異常

這裏是我的主要活動:

package com.app.conekta; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Window; 
import android.widget.Button; 
import android.widget.FrameLayout; 
import android.widget.Toast; 

public class Conekta extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

    } 

    @Override 
    public void onStart() { 

     super.onStart(); 

     FrameLayout fl1 = (FrameLayout) findViewById(R.id.headerFrameLayout); 
     //Button b=(Button) findViewById(R.id.searchButton); 
     Toast.makeText(Conekta.this, fl1.getHeight(), Toast.LENGTH_SHORT).show(); 

    } 
} 

這是我的XML:

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


    <FrameLayout 
     android:id="@+id/headerFrameLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.05" 
     android:background="#597eAA" > 

     <ImageView 
      android:id="@+id/logoImage" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="#7ba1d1" 
      android:src="@drawable/logo_conekta" /> 
    </FrameLayout> 

    <LinearLayout 
     android:id="@+id/bodyLinearLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.7" 
     android:background="#f3f3f3" 
     android:orientation="horizontal" > 

     <FrameLayout 
      android:id="@+id/leftBlankFrameLayout" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="0.15" 
      android:background="#f3f3f3" > 
     </FrameLayout> 

     <LinearLayout 
      android:id="@+id/centerVerticalLayout" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="0.7" 
      android:orientation="vertical" > 

      <FrameLayout 
       android:id="@+id/topCenterFrameLayout" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.35" > 
      </FrameLayout> 

      <TextView 
       android:id="@+id/venueLabel" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.025" 
       android:text="What are you looking for?" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:textColor="#000000" /> 

      <EditText 
       android:id="@+id/venueTextField" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.025" > 

       <requestFocus /> 
      </EditText> 

      <FrameLayout 
       android:id="@+id/middleCenterFrameLayout" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.05" > 
      </FrameLayout> 

      <TextView 
       android:id="@+id/locationLabel" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.025" 
       android:text="Where?" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:textColor="#000000" /> 

      <AutoCompleteTextView 
       android:id="@+id/locationTextField" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.025" 
       android:text="" /> 

      <LinearLayout 
       android:id="@+id/buttonLinearLayout" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.05" 
       android:background="#f3f3f3" 
       android:orientation="horizontal" > 

       <FrameLayout 
        android:id="@+id/leftButtonLinearLayout" 
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent" 
        android:layout_weight="0.1" > 
       </FrameLayout> 

       <Button 
        android:id="@+id/searchButton" 
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent" 
        android:layout_weight="0.8" 
        android:background="#6fa8dc" 
        android:text="Search" /> 

       <FrameLayout 
        android:id="@+id/rightButtonLinearLayout" 
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent" 
        android:layout_weight="0.1" > 
       </FrameLayout> 
      </LinearLayout> 

      <FrameLayout 
       android:id="@+id/bottomCenterFrameLayout" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.35" > 
      </FrameLayout> 
     </LinearLayout> 

     <FrameLayout 
      android:id="@+id/rightBlankFrameLayout" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="0.15" 
      android:background="#f3f3f3" > 
     </FrameLayout> 
    </LinearLayout> 

    <FrameLayout 
     android:id="@+id/footerFrameLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.15" 
     android:background="#7ba1d1" > 
    </FrameLayout> 

</LinearLayout> 

任何想法,爲什麼發生這種情況?

11-16 23:57:41.408: W/ResourceType(409): No package identifier when getting name for resource number 0x00000000 
11-16 23:57:41.427: D/AndroidRuntime(409): Shutting down VM 
11-16 23:57:41.427: W/dalvikvm(409): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 
11-16 23:57:41.427: E/AndroidRuntime(409): Uncaught handler: thread main exiting due to uncaught exception 
11-16 23:57:41.447: E/AndroidRuntime(409): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.conekta/com.app.conekta.Conekta}: android.content.res.Resources$NotFoundException: String resource ID #0x0 
11-16 23:57:41.447: E/AndroidRuntime(409): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 
11-16 23:57:41.447: E/AndroidRuntime(409): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
11-16 23:57:41.447: E/AndroidRuntime(409): at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
11-16 23:57:41.447: E/AndroidRuntime(409): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 
11-16 23:57:41.447: E/AndroidRuntime(409): at android.os.Handler.dispatchMessage(Handler.java:99) 
11-16 23:57:41.447: E/AndroidRuntime(409): at android.os.Looper.loop(Looper.java:123) 
11-16 23:57:41.447: E/AndroidRuntime(409): at android.app.ActivityThread.main(ActivityThread.java:4363) 
11-16 23:57:41.447: E/AndroidRuntime(409): at java.lang.reflect.Method.invokeNative(Native Method) 
11-16 23:57:41.447: E/AndroidRuntime(409): at java.lang.reflect.Method.invoke(Method.java:521) 
11-16 23:57:41.447: E/AndroidRuntime(409): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
11-16 23:57:41.447: E/AndroidRuntime(409): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
11-16 23:57:41.447: E/AndroidRuntime(409): at dalvik.system.NativeStart.main(Native Method) 
11-16 23:57:41.447: E/AndroidRuntime(409): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0 
11-16 23:57:41.447: E/AndroidRuntime(409): at android.content.res.Resources.getText(Resources.java:200) 
11-16 23:57:41.447: E/AndroidRuntime(409): at android.widget.Toast.makeText(Toast.java:258) 
11-16 23:57:41.447: E/AndroidRuntime(409): at com.app.conekta.Conekta.onStart(Conekta.java:27) 
11-16 23:57:41.447: E/AndroidRuntime(409): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129) 
11-16 23:57:41.447: E/AndroidRuntime(409): at android.app.Activity.performStart(Activity.java:3723) 
11-16 23:57:41.447: E/AndroidRuntime(409): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468) 
11-16 23:57:41.447: E/AndroidRuntime(409): ... 11 more 
11-16 23:57:41.467: I/dalvikvm(409): threadid=7: reacting to signal 3 
11-16 23:57:41.467: E/dalvikvm(409): Unable to open stack trace file '/data/anr/traces.txt': Permission denied 
+0

後異常請 – Jack

+0

Toast.makeText(Conekta.this,將String.valueOf(fl1.getHeight()),Toast.LENGTH_SHORT )。顯示(); – user370305

+0

@ user370305你知道爲什麼現在它返回一個0嗎?我知道我可能會過早地調用getHeight,所以我甚至將它移動到onStart,但我仍然可以得到它。 –

回答

1

嘗試改變

Toast.makeText(Conekta.this, fl1.getHeight(), Toast.LENGTH_SHORT).show(); 

Toast.makeText(Conekta.this, String.valueOf(fl1.getHeight()), Toast.LENGTH_SHORT).show(); 
+0

那就解決了。謝謝!我愚蠢的不要去嘗試一下。 –

+0

如果正確,你應該接受這個答案。您可以通過單擊答案旁邊的複選標記來完成此操作。 –