Android - Random XML Inflate Errors
Stack overflow has been great to me in the past week in developing my first android app. Im having a very random problem though. Sometimes my app has a problem inflating the XML layout files. It's not one particular one, but when I change an XML file, it randomly force closes because I need to define layout_height on line 18. At least that's what it usually tells me. I normally just rewrite that View tag and it works fine but I would like some insight on to whats going on. Any help is appreciated!
Heres a sample XML that FCs because of the problem above.
<?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>
Edit, 3/17/2011 5:05PM CST:
Here is the activity that is being created on the 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.LENG开发者_开发百科TH_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();
}
});
}
}
The LogCat when the activity FC's:
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
And lastly, the thread stack:
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]
Brian,
Perhaps problem is with dateTimeValue
you passed it to CreateNote()
without ever initializing it.
The problem can be in your build process. Just make cleaning before making project should solve your problems.
精彩评论