我是Android编程的新手,我正在尝试制作一款用于管理日常任务的应用。现在我的应用程序需要在活动之间切换,它通常在第一个活动上启动,但是当我单击按钮转到其他活动时,应用程序就崩溃了。我正在直接调试我的手机(Galaxy s3)。
我认为logcat输出可能很有用,因为我注意到在其他问题中它有很多帮助。
09-24 05:49:14.807: D/dalvikvm(9062): GC_FOR_ALLOC freed 137K, 4% free 12835K/13255K, paused 25ms
09-24 05:49:14.807: I/dalvikvm-heap(9062): Grow heap (frag case) to 13.065MB for 379472-byte allocation
09-24 05:49:14.837: D/dalvikvm(9062): GC_FOR_ALLOC freed <1K, 4% free 13205K/13639K, paused 11ms
09-24 05:49:14.862: D/AbsListView(9062): Get MotionRecognitionManager
09-24 05:49:16.102: D/CLIPBOARD(9062): Hide Clipboard dialog at Starting input: finished by someone else... !
09-24 05:49:22.282: D/AndroidRuntime(9062): Shutting down VM
09-24 05:49:22.282: W/dalvikvm(9062): threadid=1: thread exiting with uncaught exception (group=0x40c661f8)
09-24 05:49:22.297: E/AndroidRuntime(9062): FATAL EXCEPTION: main
09-24 05:49:22.297: E/AndroidRuntime(9062): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dailyplanner/com.example.dailyplanner.new_task}: java.lang.RuntimeException: Binary XML file line #24: You must supply a layout_width attribute.
09-24 05:49:22.297: E/AndroidRuntime(9062): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
09-24 05:49:22.297: E/AndroidRuntime(9062): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
09-24 05:49:22.297: E/AndroidRuntime(9062): at android.app.ActivityThread.access$600(ActivityThread.java:128)
09-24 05:49:22.297: E/AndroidRuntime(9062): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
09-24 05:49:22.297: E/AndroidRuntime(9062): at android.os.Handler.dispatchMessage(Handler.java:99)
09-24 05:49:22.297: E/AndroidRuntime(9062): at android.os.Looper.loop(Looper.java:137)
09-24 05:49:22.297: E/AndroidRuntime(9062): at android.app.ActivityThread.main(ActivityThread.java:4514)
09-24 05:49:22.297: E/AndroidRuntime(9062): at java.lang.reflect.Method.invokeNative(Native Method)
09-24 05:49:22.297: E/AndroidRuntime(9062): at java.lang.reflect.Method.invoke(Method.java:511)
09-24 05:49:22.297: E/AndroidRuntime(9062): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
09-24 05:49:22.297: E/AndroidRuntime(9062): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
09-24 05:49:22.297: E/AndroidRuntime(9062): at dalvik.system.NativeStart.main(Native Method)
09-24 05:49:22.297: E/AndroidRuntime(9062): Caused by: java.lang.RuntimeException: Binary XML file line #24: You must supply a layout_width attribute.
09-24 05:49:22.297: E/AndroidRuntime(9062): at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)
09-24 05:49:22.297: E/AndroidRuntime(9062): at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:5331)
09-24 05:49:22.297: E/AndroidRuntime(9062): at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:5452)
09-24 05:49:22.297: E/AndroidRuntime(9062): at android.widget.RelativeLayout$LayoutParams.<init>(RelativeLayout.java:1064)
09-24 05:49:22.297: E/AndroidRuntime(9062): at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:933)
09-24 05:49:22.297: E/AndroidRuntime(9062): at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:69)
09-24 05:49:22.297: E/AndroidRuntime(9062): at android.view.LayoutInflater.rInflate(LayoutInflater.java:741)
09-24 05:49:22.297: E/AndroidRuntime(9062): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
09-24 05:49:22.297: E/AndroidRuntime(9062): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-24 05:49:22.297: E/AndroidRuntime(9062): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-24 05:49:22.297: E/AndroidRuntime(9062): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:280)
09-24 05:49:22.297: E/AndroidRuntime(9062): at android.app.Activity.setContentView(Activity.java:1892)
09-24 05:49:22.297: E/AndroidRuntime(9062): at com.example.dailyplanner.new_task.onCreate(new_task.java:23)
09-24 05:49:22.297: E/AndroidRuntime(9062): at android.app.Activity.performCreate(Activity.java:4562)
09-24 05:49:22.297: E/AndroidRuntime(9062): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
09-24 05:49:22.297: E/AndroidRuntime(9062): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
09-24 05:49:22.297: E/AndroidRuntime(9062): ... 11 more
09-24 05:49:22.317: D/dalvikvm(9062): GC_CONCURRENT freed 164K, 3% free 13459K/13831K, paused 1ms+2ms
09-24 05:49:22.322: E/SQLiteDatabase(9062): close() was never explicitly called on database '/data/data/com.example.dailyplanner/databases/MyDB'
09-24 05:49:22.322: E/SQLiteDatabase(9062): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
09-24 05:49:22.322: E/SQLiteDatabase(9062): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:2064)
09-24 05:49:22.322: E/SQLiteDatabase(9062): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1118)
09-24 05:49:22.322: E/SQLiteDatabase(9062): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1075)
09-24 05:49:22.322: E/SQLiteDatabase(9062): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1051)
09-24 05:49:22.322: E/SQLiteDatabase(9062): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1154)
09-24 05:49:22.322: E/SQLiteDatabase(9062): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1147)
09-24 05:49:22.322: E/SQLiteDatabase(9062): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:860)
09-24 05:49:22.322: E/SQLiteDatabase(9062): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:222)
09-24 05:49:22.322: E/SQLiteDatabase(9062): at com.example.dailyplanner.MainActivity.onCreate(MainActivity.java:24)
09-24 05:49:22.322: E/SQLiteDatabase(9062): at android.app.Activity.performCreate(Activity.java:4562)
09-24 05:49:22.322: E/SQLiteDatabase(9062): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
09-24 05:49:22.322: E/SQLiteDatabase(9062): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
09-24 05:49:22.322: E/SQLiteDatabase(9062): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
09-24 05:49:22.322: E/SQLiteDatabase(9062): at android.app.ActivityThread.access$600(ActivityThread.java:128)
09-24 05:49:22.322: E/SQLiteDatabase(9062): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
09-24 05:49:22.322: E/SQLiteDatabase(9062): at android.os.Handler.dispatchMessage(Handler.java:99)
09-24 05:49:22.322: E/SQLiteDatabase(9062): at android.os.Looper.loop(Looper.java:137)
09-24 05:49:22.322: E/SQLiteDatabase(9062): at android.app.ActivityThread.main(ActivityThread.java:4514)
09-24 05:49:22.322: E/SQLiteDatabase(9062): at java.lang.reflect.Method.invokeNative(Native Method)
09-24 05:49:22.322: E/SQLiteDatabase(9062): at java.lang.reflect.Method.invoke(Method.java:511)
09-24 05:49:22.322: E/SQLiteDatabase(9062): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
09-24 05:49:22.322: E/SQLiteDatabase(9062): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
09-24 05:49:22.322: E/SQLiteDatabase(9062): at dalvik.system.NativeStart.main(Native Method)`
这是我的android清单:
<manifest package="com.example.dailyplanner"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res
/android" android:versionCode="1">
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Light.NoTitleBar" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".new_task"/>
</application>
`
这里是2个java文件的代码:
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SQLiteDatabase db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null );
db.execSQL("CREATE TABLE IF NOT EXISTS Data1 (Id INT IDENTITY(1,1) NOT NULL, text VARCHAR, time TIME, date DATE)");
Button nTaskBtn = (Button) findViewById(R.id.nTask);
nTaskBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent nTaskScreen = new Intent(MainActivity.this, new_task.class);
startActivity(nTaskScreen);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
第二档:
import android.R.string;
import android.app.Activity;
import android.app.TimePickerDialog;
import android.net.UrlQuerySanitizer.ValueSanitizer;
import android.os.Bundle;
import android.renderscript.Sampler.Value;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
public class new_task extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.new_task);
TextView timeTxt = (TextView) findViewById(R.id.timeTxt);
TextView dateTxt = (TextView) findViewById(R.id.dateTxt);
Button btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
Button btnSetTime = (Button) findViewById(R.id.btnSetTime);
btnSetTime.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
}
}
任何帮助将不胜感激:)
答案 0 :(得分:1)
logcat输出解释了这些问题。首先,在文件res/layout/activity_main
中,似乎(至少)有一个视图没有必需的android:layout_width
属性。
另一个问题是您在MainActivity.onCreate
打开数据库并且永远不会关闭它。你需要关闭它。
答案 1 :(得分:1)
清楚地看一下logcat错误,尤其是这一行..
09-24 05:49:22.297: E/AndroidRuntime(9062): Caused by: java.lang.RuntimeException: Binary XML file line #24: You must supply a layout_width attribute.
答案 2 :(得分:0)
您必须为布局提供必要的attributes.layout_height和layout_width