how to switch to activity from listactivity using menu
my initial screen is basically from a listactivity which displays a list of records. i created a menu that should open a new window for entry but always force closes when i click it. below are my codes:
Guestbook.java (ListActivity)
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.mnuNew:
//setContentView(R.layout.form);
Intent mIntent = new Intent(getApplicationContext(), Maintenance.class);
startActivityForResult(mIntent, 0);
//Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
here is the code for Maintenance.class
package com.android.guestbook;
import android.app.Activity;
import android.os.Bundle;
public class Maintenance extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form);
}
}
is there something wrong with my code?
here is the logcat info.
02-06 07:02:29.617: INFO/ActivityManager(59): Starting activity: Intent { cmp=com.android.guestbook/.Maintenance }
02-06 07:02:29.956: DEBUG/AndroidRuntime(276): Shutting down VM
02-06 07:02:29.956: WARN/dalvikvm(276): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): FATAL EXCEPTION: main
02-06 07:02:30.107: ERROR/AndroidRuntime(276): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.guestbook/com.android.guestbook.Maintenance}: java.lang.RuntimeException: Binary XML file line #82: You must supply a layout_width attribute.
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at android.os.Looper.loop(Looper.java:123)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at java.lang.reflect.Method.invokeNative(Native Method)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at java.lang.reflect.Method.invoke(Method.java:521)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at dalvik.system.NativeStart.main(Native Method)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): Caused by: java.lang.RuntimeException: Binary XML file line #82: You must supply a layout_width attribute.
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:3592)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:3672)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1395)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1321)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:45)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at android.view.LayoutInflater.rInflate(LayoutInflater.java:620)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at android.app.Activity.setContentView(Activity.java:1647)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at com.android.guestbook.Maintenance.onCreate(Maintenance.java:11)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-06 07:02:30.107: ERROR/AndroidRuntime(276): ... 11 more
02-06 07:02:30.216: WARN/ActivityManager(59): Force finishing activity com.android.guestbook/.Maintenance
02-06 07:02:30.267: WARN/ActivityManager(59): Force finishing activity com.android.guestbook/.GuestBook
here is my form.xml for the Maintenance class.
> <?xml version="1.0" encoding="utf-8"?>
> <LinearLayout
> xmlns:android="http://schemas.android.com/apk/res/android"
> android:layout_width="fill_parent"
> android:layout_height="fill_parent"
> android:orientation="vertical">
> <LinearLayout
> android:layout_height="wrap_content"
> android:layout_width="fill_parent">
> <TextView
> android:layout_width="fill_parent"
> android:layout_height="wrap_content"
> android:text="@string/name" />
> </LinearLayout>
> <LinearLayout
> android:layout_height="wrap_content"
> android:layout_width="fill_parent">
> <EditText
> android:inputType="textPersonName"
> android:layout_width="fill_parent"
> android:layout_height="wrap_content"
> android:id="@+id/name"
> android:hint="Enter your name here" />
> </LinearLayout>
> <LinearLayout
> android:layout_height="wrap_content"
> android:layout_width="fill_parent">
> <TextView
> android:layout_width="wrap_content"
> android:layout_height="wrap_content"
> android:text="@string/email" />
> </LinearLayout>
> <LinearLayout
> android:layout_height="wrap_content"
> android:layout_width="fill_parent">
> <EditText
> android:inputType="textEmailAddress"
> android:layout_width="fill_parent"
> android:layout_height="wrap_content"
> android:id="@+id/email"
> android:hint="Enter your email here" />
> </LinearLayout>
> <LinearLayout
> android:layout_height="wrap_content"
> android:layout_width="开发者_开发技巧fill_parent">
> <TextView
> android:layout_width="wrap_content"
> android:layout_height="wrap_content"
> android:text="@string/message" />
> </LinearLayout>
> <LinearLayout
> android:layout_height="wrap_content"
> android:layout_width="fill_parent">
> <EditText
> android:inputType="textMultiLine"
> android:layout_width="fill_parent"
> android:layout_height="wrap_content"
> android:id="@+id/message"
> android:hint="Enter your message here"></EditText>
> </LinearLayout>
> <LinearLayout
> android:layout_height="wrap_content"
> android:layout_width="fill_parent">
> <Button
> android:onClick="onClick"
> android:layout_weight="1"
> android:layout_width="fill_parent"
> android:layout_height="wrap_content"
> android:id="@+id/submit"
> android:text="@string/submit" />
> <Button
> android:onClick="onClick"
> android:layout_weight="1"
> android:layout_width="fill_parent"
> android:layout_height="wrap_content"
> android:id="@+id/cancel"
> android:text="@string/cancel" />
> </LinearLayout>
> <ListView
> android:id="@+id/list" /> </LinearLayout>
Did you declare your Maintenance.class - Activity in your Manifest file?!
You need to put the <activity>
-Tag in your AndroidManifest.xml
between the <application>
-Tag:
<application android:label="@string/app_name" android:icon="@drawable/icon" android:debuggable="true">
<activity android:name=".activities.Guestbook"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.Maintenance "
android:label="@string/app_name">
</activity>
</application>
thanks to all your help i finally figured out what's wrong. checking the logcat it said some issue about the layout_width of my xml layout. so then i checked the xml and noticed that i had a listview without any layout_width property. once i took it off it started to work.
thanks again.
One error is that declare following two parameter of ListView widget at 82 line of form.xml.
android:layout_height=
android:layout_width=
精彩评论