我正在使用Achartengine for Android,我有一个由LinearLayout中的ScrollView母亲组成的布局。它包含一个LinearLayout,它是Graph和一些textview e edittext。
问题是,如果我在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的顶部)和private,实际上它们在图形位于顶部时运行。
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。
答案 0 :(得分:1)
我解决了向LinearLayout父级添加ID的问题:
android:id="@+id/general"
我不知道它为什么会起作用,所以我感觉不舒服,因为我担心它是否会在将来发挥作用。 也许线性布局之间存在冲突。 如果有人理解,解决方案可以写在这里。
谢谢你的帮助! @mango