2011-03-17 45 views
0

在過去一週裏,堆棧溢出對我來說非常棒,開發我的第一個android應用程序。我有一個非常隨機的問題,但。有時我的應用程序在擴展XML佈局文件時遇到了問題。這不是特別的,但是當我更改XML文件時,它隨機強制關閉,因爲我需要在第18行定義layout_height。至少這是它通常告訴我的。我通常只是重寫視圖標籤,它的工作正常,但我想了解一些發生了什麼。任何幫助表示讚賞!Android - 隨機XML膨脹錯誤

繼承人由於上述問題而出現FC的示例XML。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/bg" 
    > 
<TextView 
    android:id="@+id/noteFormDateTimeTest" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#000000" 
    android:textStyle="bold" 
    android:textSize="26dp" /> 
<TextView 
    android:id="@+id/noteFormContact" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" <!--This would be line 18--> 
    android:textColor="#000000" 
    android:textStyle="bold" 
    android:textSize="26dp" 
    android:layout_below="@id/noteFormDateTimeTest"/> 
<EditText 
    android:id="@+id/noteFormTitle" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:hint="Note Title" 
    android:layout_below="@id/noteFormContact"/> 
<EditText 
    android:id="@+id/noteFormBody" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:singleLine="false" 
    android:lines="15" 
    android:gravity="top" 
    android:hint="Enter Text Here" 
    android:layout_below="@id/noteFormTitle" /> 
<View 
    android:id="@+id/helper" 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" /> 
<Button 
    android:id="@+id/noteFormSave" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="Save" 
    android:layout_alignParentBottom="true" 
    android:layout_toLeftOf="@id/helper"/> 
<Button 
    android:id="@+id/noteFormDiscard" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="Discard" 
    android:layout_alignParentBottom="true" 
    android:layout_toRightOf="@id/helper"/> 
</RelativeLayout> 

編輯,2011/3/17下午5時05分CST:

這裏是正在對FC創建活動:

package com.onyx.formapp23; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.text.format.DateFormat; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class FormsNewNote extends Activity {  
    String intentRowId, mIntentName, mIntentId, intentTitle, intentBody, dateTimeValue; 
    int contactId; 
    TextView contactHeader, dateTimeText; 
    EditText titleEdit, bodyEdit; 
    long longRowId; 
    Button submitBtn, discardBtn; 
    private MyDbAdapter mDb = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.forms_newnote); 
     Bundle extras = getIntent().getExtras(); 
     intentRowId = extras.getString("rowId"); 
     if (intentRowId != null){ 
      longRowId = Long.decode(intentRowId); 
     } 
     mIntentId = extras.getString("contactId"); 
     mIntentName = extras.getString("contactName"); 
     intentTitle = extras.getString("title"); 
     intentBody = extras.getString("body"); 
     mDb = new MyDbAdapter(this); 

     contactHeader = (TextView) findViewById(R.id.noteFormContact); 
     contactHeader.setText(mIntentId + ": " + mIntentName); 

     dateTimeText = (TextView) findViewById(R.id.noteFormDateTimeTest); 
     DateFormat dateFormat = (DateFormat) DateFormat.format("MM-dd-yyyy hh:mm:ss", new java.util.Date()); 
     dateTimeText.setText((CharSequence) dateFormat); 

     titleEdit = (EditText) findViewById(R.id.noteFormTitle); 
     if (intentTitle != null) { 
      titleEdit.setText(intentTitle); 
     } 
     bodyEdit = (EditText) findViewById(R.id.noteFormBody); 
     if (intentBody != null) { 
      bodyEdit.setText(intentBody); 
     } 

     submitBtn = (Button) findViewById(R.id.noteFormSave); 
     submitBtn.setOnClickListener(new OnClickListener(){ 
      @Override 
      public void onClick(View v) { 
       String titleInsert = titleEdit.getText().toString(); 
       String bodyInsert = bodyEdit.getText().toString(); 
       if(titleInsert == null || bodyInsert == null){ 
        Context context = getApplicationContext(); 
        CharSequence text = "Please fill all fields before saving"; 
        int duration = Toast.LENGTH_SHORT; 
        Toast toast = Toast.makeText(context, text, duration); 
        toast.show(); 
       } else if (intentRowId == null && titleInsert != null && bodyInsert != null){ 
        try{ 
        mDb.open(); 
        mDb.createNote(Integer.parseInt(mIntentId), titleInsert, bodyInsert, dateTimeValue); 
        finish(); 
        } catch (Exception e) { 
        e.printStackTrace(); 
        } 
       } else { 
        try{ 
         mDb.open(); 
         mDb.updateNote(longRowId, titleInsert, bodyInsert); 
         finish(); 
        } catch (Exception e){ 
         e.printStackTrace(); 
         Context context = getApplicationContext(); 
         CharSequence text = "SQL Exception Thrown: " + e; 
         int duration = Toast.LENGTH_SHORT; 
         Toast toast = Toast.makeText(context, text, duration); 
         toast.show(); 
        } 
       } 
       } 
     }); 
     discardBtn = (Button) findViewById(R.id.noteFormDiscard); 
     discardBtn.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v){ 
       finish(); 
      } 
     }); 
    } 


} 

的logcat的活動時FC的:

I/ActivityManager( 316): Starting activity: Intent { cmp=com.onyx.formapp23/.FormsNewNote (has extras) } 

D/AndroidRuntime(10518): Shutting down VM 

W/dalvikvm(10518): threadid=1: thread exiting with uncaught exception (group=0x40138820) 

E/AndroidRuntime(10518): FATAL EXCEPTION: main 

E/AndroidRuntime(10518): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.onyx.formapp23/com.onyx.formapp23.FormsNewNote}: java.lang.ClassCastException: java.lang.String 

E/AndroidRuntime(10518): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2664) 

E/AndroidRuntime(10518): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2680) 

E/AndroidRuntime(10518): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 

E/AndroidRuntime(10518): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2034) 

E/AndroidRuntime(10518): at android.os.Handler.dispatchMessage(Handler.java:99) 

E/AndroidRuntime(10518): at android.os.Looper.loop(Looper.java:123) 

E/AndroidRuntime(10518): at android.app.ActivityThread.main(ActivityThread.java:4628) 

E/AndroidRuntime(10518): at java.lang.reflect.Method.invokeNative(Native Method) 

E/AndroidRuntime(10518): at java.lang.reflect.Method.invoke(Method.java:521) 

E/AndroidRuntime(10518): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:870) 

E/AndroidRuntime(10518): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628) 

E/AndroidRuntime(10518): at dalvik.system.NativeStart.main(Native Method) 

E/AndroidRuntime(10518): Caused by: java.lang.ClassCastException: java.lang.String 

E/AndroidRuntime(10518): at com.onyx.formapp23.FormsNewNote.onCreate(FormsNewNote.java:42) 

E/AndroidRuntime(10518): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 

E/AndroidRuntime(10518): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2628) 

E/AndroidRuntime(10518): ... 11 more 

W/ActivityManager( 316): Force finishing activity com.onyx.formapp23/.FormsNewNote 

W/ActivityManager( 316): Force finishing activity com.onyx.formapp23/.NotesList 

最後,線程堆棧:

Thread [<1> main] (Suspended (exception RuntimeException)) 
    ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2664 
    ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2680 
    ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 125 
    ActivityThread$H.handleMessage(Message) line: 2034 
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 123 
    ActivityThread.main(String[]) line: 4628  
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method] 
    Method.invoke(Object, Object...) line: 521 
    ZygoteInit$MethodAndArgsCaller.run() line: 870 
    ZygoteInit.main(String[]) line: 628 
    NativeStart.main(String[]) line: not available [native method] 
+0

你可以發送確切的錯誤消息,或最好是logcat? – EboMike 2011-03-17 19:05:38

回答

0

布賴恩,

也許問題是dateTimeValue你把它傳遞給CreateNote()而沒有對其進行初始化。

+0

這似乎是它。雖然我不明白爲什麼它顯示爲XML問題,在註釋掉日期/時間代碼後,它加載得很好如果你有第二個解釋它爲什麼會這樣做,我會很感激,我是編程新手,並且還沒有完全理解所有的東西,但我喜歡它,並且希望儘可能地理解它。 – 2011-03-17 23:16:04

+0

你的'logcat'這一行讓我相信這是你的字符串的一個問題:'E/AndroidRuntime(10518):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.onyx.formapp23/com.onyx。 formapp23.FormsNewNote}:java.lang.ClassCastException:java.lang.String'然後從那裏檢查你聲明的每個'String'變量,以確保它們在null或未初始化的時候都不被訪問 – 2011-03-17 23:18:21

+0

Sorry for late反應信息的anks。希望這能幫助我在未來找出這些問題或問題。 – 2011-03-22 16:19:45

0

問題可能出現在您的構建過程中。在做項目應該解決您的問題之前,做清潔。

+0

嘗試清理和Android>多次修復屬性,沒有運氣:( – 2011-03-17 22:18:39