2011-03-17 32 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