【发布时间】:2011-07-19 22:08:42
【问题描述】:
我正在尝试在我的 Android 应用程序中绘制自定义形状。我使用了这个http://bestsiteinthemultiverse.com/2008/11/android-graphics-example/ 示例,如果我唯一拥有的是这个组件,我可以让它与我的应用程序一起工作。但是,我需要使用自定义形状和 2 个文本视图和 2 个按钮。每当我尝试使用 Buttons 和 TextViews 在我的应用程序中创建此组件时,应用程序都会失败。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<Button android:id="@+id/back" android:layout_width="160sp"
android:textSize="20sp" android:layout_height="wrap_content"
android:text="Previous" />
<Button android:id="@+id/next" android:layout_width="160sp"
android:textSize="20sp" android:layout_height="wrap_content"
android:text="Next" />
</LinearLayout>
<TextSwitcher android:id="@+id/titles"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TextSwitcher android:id="@+id/data"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<com.android.example.DemoView android:id="@+id/demoView"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<Button android:id="@+id/info" android:layout_width="fill_parent"
android:textSize="20sp" android:layout_height="wrap_content"
android:text="About this App" />
</LinearLayout>
</ScrollView>
那是我的布局 XML 文件,我有点怀疑这是问题所在。什么可能导致错误?
这里是 LogCar 错误日志。
03-21 19:34:08.810: 错误/AndroidRuntime(608): 致命异常: main
03-21 19:34:08.810: 错误/AndroidRuntime(608): java.lang.RuntimeException: 无法启动活动 ComponentInfo{android.project.realtimedata/android.project.realtimedata.ViewData}: java.lang.NullPointerException
03-21 19:34:08.810: 错误/AndroidRuntime(608): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-21 19:34:08.810: 错误/AndroidRuntime(608): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-21 19:34:08.810: 错误/AndroidRuntime(608): 在 android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-21 19:34:08.810: 错误/AndroidRuntime(608): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-21 19:34:08.810: 错误/AndroidRuntime(608): 在 android.os.Handler.dispatchMessage(Handler.java:99)
03-21 19:34:08.810: 错误/AndroidRuntime(608): 在 android.os.Looper.loop(Looper.java:123)
03-21 19:34:08.810: 错误/AndroidRuntime(608): 在 android.app.ActivityThread.main(ActivityThread.java:4627)
03-21 19:34:08.810: 错误/AndroidRuntime(608): 在 java.lang.reflect.Method.invokeNative(Native Method)
03-21 19:34:08.810: 错误/AndroidRuntime(608): 在 java.lang.reflect.Method.invoke(Method.java:521)
03-21 19:34:08.810: 错误/AndroidRuntime(608): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-21 19:34:08.810: 错误/AndroidRuntime(608): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-21 19:34:08.810: 错误/AndroidRuntime(608): at dalvik.system.NativeStart.main(Native Method)
03-21 19:34:08.810: 错误/AndroidRuntime(608): 原因: java.lang.NullPointerException
03-21 19:34:08.810: 错误/AndroidRuntime(608): 在 android.project.realtimedata.ViewData.onCreate(ViewData.java:77)
03-21 19:34:08.810: 错误/AndroidRuntime(608): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-21 19:34:08.810: 错误/AndroidRuntime(608): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-21 19:34:08.810: 错误/AndroidRuntime(608): ... 11 更多
【问题讨论】:
-
在应用崩溃后粘贴 logcat。
-
添加 logcat 输出,没有那个很难猜到
-
所以你有一个致命的异常......由
NullPointerException在你的ViewData.java的第77行引发。您能否发布您的onCreate()方法并告诉我们哪一个是第 77 行?
标签: android components