在过去的一周里,堆栈溢出对我开发我的第一个Android应用程序非常好。我有一个非常随机的问题。有时我的应用程序在扩展XML布局文件时遇到问题。它不是一个特定的,但是当我更改XML文件时,它随机强制关闭,因为我需要在第18行定义layout_height。至少这是它通常告诉我的。我通常只是重写View标签,它工作正常,但我想了解最新情况。任何帮助表示赞赏!
由于上述问题,下面是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();
}
});
}
}
活动FC的LogCat:
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 :(得分:0)
问题出在你的构建过程中。在制作项目之前先做清洁就可以解决问题。
答案 1 :(得分:0)
布赖恩,
也许问题在于dateTimeValue
您将其传递给CreateNote()
而没有初始化它。