1

我正在使用适用于 Android 的 Achartengine,并且我的布局由 LinearLayout 中的 ScrollView 母亲组成。它包含一个线性布局,它是图形和一些文本视图和编辑文本。

问题是,如果我在 LinearLayout 的顶部插入图形然后程序运行,但如果我在底部插入图形,在所有 textview 和 edittext 之后程序崩溃。为什么?

这是我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <!-- android:layout_weight="1" -->

        <LinearLayout
            android:id="@+id/chart"
            android:layout_width="fill_parent"
            android:layout_height="200dp"
            android:layout_margin="2dp"
            android:orientation="horizontal" />

        <TextView
            android:id="@+id/txtX"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="X: 0"
            android:textSize="15sp" >
        </TextView>

        <TextView
            android:id="@+id/txtZ"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="39dp"
            android:text="Z: 0"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/txtY"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="35dp"
            android:text="Y: 0"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/txtM"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="35dp"
            android:text="M: 0"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/txtM2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="35dp"
            android:text="M: 0"
            android:textSize="15sp" />

        <EditText
            android:id="@+id/et1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="39dp"
            android:ems="10"
            android:focusable="false"
            android:inputType="textMultiLine" >
        </EditText>
    </LinearLayout>

</ScrollView>

谢谢!:)

PS:这是应用程序崩溃时的LogCat:

12-31 13:56:26.839: I/ActivityManager(169): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.spettro1/.MainActivity } from pid 2688
12-31 13:56:26.849: I/Launcher(2252): onPause()
12-31 13:56:26.859: D/AndroidRuntime(2688): Shutting down VM
12-31 13:56:26.889: I/AndroidRuntime(2688): NOTE: attach of thread 'Binder Thread #3' failed
12-31 13:56:26.899: I/ActivityManager(169): Start proc com.spettro1 for activity com.spettro1/.MainActivity: pid=2697 uid=10125 gids={}
12-31 13:56:26.969: I/OrientationDebug(169): [pwm] in updateOrientationListenerLp()
12-31 13:56:26.969: V/OrientationDebug(169): in updateOrientationListenerLp(), Screen status=true, current orientation=-1, SensorEnabled=true
12-31 13:56:26.969: I/OrientationDebug(169): [pwm] needSensorRunningLp(), return true #4
12-31 13:56:26.979: I/Launcher(2252): onWindowFocusChanged(false)
12-31 13:56:26.989: D/Launcher(2252): setWallpaperDimension() called
12-31 13:56:26.989: D/Launcher(2252): USE_NON_SCROLLABLE_IMAGE_WALLPAPER is true
12-31 13:56:27.019: D/Launcher(2252): It's image wallpaper. suggestDesiredDimensions(-1,-1)
12-31 13:56:27.039: D/SensorManager(2697): ====>>>>>Num Sensor: 1
12-31 13:56:27.039: D/SensorManager(2697): ====>>>>>Num Sensor: 2
12-31 13:56:27.039: D/SensorManager(2697): ====>>>>>Num Sensor: 3
12-31 13:56:27.039: D/SensorManager(2697): ====>>>>>Num Sensor: 4
12-31 13:56:27.039: D/SensorManager(2697): ====>>>>>Num Sensor: 5
12-31 13:56:27.039: D/SensorManager(2697): ====>>>>>Num Sensor: 6
12-31 13:56:27.039: D/SensorManager(2697): ====>>>>>Num Sensor: 0
12-31 13:56:27.039: E/SensorManager(2697): registerListener :: handle = 1600940398  name= Linear Acceleration Sensor delay= 10000 Listener= com.spettro1.MainActivity@40519c70
12-31 13:56:27.049: E/SensorManager(2697): =======>>>Sensor Thread RUNNING <<<========
12-31 13:56:27.049: E/SensorHAL(169): +__poll_activate: handle=0 enabled=1
12-31 13:56:27.119: D/AndroidRuntime(2697): Shutting down VM
12-31 13:56:27.119: W/dalvikvm(2697): threadid=1: thread exiting with uncaught exception (group=0x40018578)
12-31 13:56:27.139: E/AndroidRuntime(2697): FATAL EXCEPTION: main
12-31 13:56:27.139: E/AndroidRuntime(2697): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.spettro1/com.spettro1.MainActivity}: java.lang.ClassCastException: android.widget.LinearLayout
12-31 13:56:27.139: E/AndroidRuntime(2697):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
12-31 13:56:27.139: E/AndroidRuntime(2697):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-31 13:56:27.139: E/AndroidRuntime(2697):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-31 13:56:27.139: E/AndroidRuntime(2697):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-31 13:56:27.139: E/AndroidRuntime(2697):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-31 13:56:27.139: E/AndroidRuntime(2697):     at android.os.Looper.loop(Looper.java:130)
12-31 13:56:27.139: E/AndroidRuntime(2697):     at android.app.ActivityThread.main(ActivityThread.java:3687)
12-31 13:56:27.139: E/AndroidRuntime(2697):     at java.lang.reflect.Method.invokeNative(Native Method)
12-31 13:56:27.139: E/AndroidRuntime(2697):     at java.lang.reflect.Method.invoke(Method.java:507)
12-31 13:56:27.139: E/AndroidRuntime(2697):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-31 13:56:27.139: E/AndroidRuntime(2697):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-31 13:56:27.139: E/AndroidRuntime(2697):     at dalvik.system.NativeStart.main(Native Method)
12-31 13:56:27.139: E/AndroidRuntime(2697): Caused by: java.lang.ClassCastException: android.widget.LinearLayout
12-31 13:56:27.139: E/AndroidRuntime(2697):     at com.spettro1.MainActivity.initComponents(MainActivity.java:112)
12-31 13:56:27.139: E/AndroidRuntime(2697):     at com.spettro1.MainActivity.onCreate(MainActivity.java:94)
12-31 13:56:27.139: E/AndroidRuntime(2697):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-31 13:56:27.139: E/AndroidRuntime(2697):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
12-31 13:56:27.139: E/AndroidRuntime(2697):     ... 11 more
12-31 13:56:27.139: W/ActivityManager(169):   Force finishing activity com.spettro1/.MainActivity
12-31 13:56:27.149: E/(169): Dumpstate > /data/log/dumpstate_app_error
12-31 13:56:27.649: W/ActivityManager(169): Activity pause timeout for HistoryRecord{4082f818 com.spettro1/.MainActivity}

这是 initComponents() 内 112 行附近的崩溃代码:

        txtX = (TextView) findViewById(R.id.txtX);
        txtY = (TextView) findViewById(R.id.txtY);
        txtZ = (TextView) findViewById(R.id.txtZ);
        txtM = (TextView) findViewById(R.id.txtM);
        txtM2 = (TextView) findViewById(R.id.txtM2); 
112     ed1 = (EditText) findViewById(R.id.et1); 

        for(int i=0; i<camp.length; ++i)
            camp[i]=0;

注意:textview 和 edittext 的定义是全局的(在 mainactivity 的顶部)和私有的,事实上它们在图形位于顶部时运行。

Edit1:我尝试将 XML 中的第一个 LinearLayout 更改为 TableLayout,然后我验证问题不是第一个 LinearLayout,而是我实现图形的 LinearLaoyut:

LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
mChartView = ChartFactory.getBarChartView(getBaseContext(), dataset, multiRenderer, BarChart.Type.DEFAULT);
layout.addView(mChartView);

这在编译器标记错误的 initComponents() 中调用。我试图在 textview 声明之前插入这个声明和实例化,唯一改变的是 logcat 没有标记任何指定的错误,只是在 ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) 行崩溃:1651。

4

1 回答 1

2

我解决了向 LinearLayout 父级添加 ID 的问题:

android:id="@+id/general"

我不知道它为什么会起作用,所以我感觉很糟糕,因为我担心它将来是否会起作用。也许线性布局之间存在冲突。如果有人理解解决方案可以在这里写。

谢谢你帮助我!@芒果

于 2013-01-10T15:45:37.457 回答