当我试图在模拟器中加载Android应用程序时,它崩溃了
我对Android编程非常陌生,而且我的起步很艰难。我已经为一个程序创建了一个通用UI,并且没有更改任何实际的Java代码。每当我试图在emulator中加载应用程序以查看其外观时,应用程序就会崩溃。有人知道为什么会发生这种情况吗 另外,我根据教程制作的其他应用程序也可以很好地工作,只是我最近为其创建了一个布局的应用程序不起作用 下面是Java代码当我试图在模拟器中加载Android应用程序时,它崩溃了,android,Android,我对Android编程非常陌生,而且我的起步很艰难。我已经为一个程序创建了一个通用UI,并且没有更改任何实际的Java代码。每当我试图在emulator中加载应用程序以查看其外观时,应用程序就会崩溃。有人知道为什么会发生这种情况吗 另外,我根据教程制作的其他应用程序也可以很好地工作,只是我最近为其创建了一个布局的应用程序不起作用 下面是Java代码 package com.nubs.tableposv0_1; import android.app.Activity; import androi
package com.nubs.tableposv0_1;
import android.app.Activity;
import android.os.Bundle;
public class TablePOSv0_1Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
下面是更新的XML代码
<?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="horizontal"
android:weightSum="5"
>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
>
<Button android:id="@+id/item1"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Item 1"
/>
<Button android:id="@+id/item2"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Item 2"
/>
<Button android:id="@+id/item3"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Item 3"
/>
<Button android:id="@+id/item4"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Item 4"
/>
<Button android:id="@+id/item5"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Item 5"
/>
</LinearLayout>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
>
<Button android:id="@+id/item6"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Item 6"
/>
<Button android:id="@+id/item7"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Item 7"
/>
<Button android:id="@+id/item8"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Item 8"
/>
<Button android:id="@+id/item9"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Item 9"
/>
<Button android:id="@+id/item10"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Item 10"
/>
</LinearLayout>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="3"
android:orientation="vertical"
android:weightSum="9"
>
<TableLayout
android:id="@+id/items_table"
android:layout_width="fill_parent"
android:layout_weight="4"
>
</TableLayout>
<LinearLayout
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_weight="1"
android:weightSum="3"
>
<Button android:id="@+id/calc1"
android:layout_weight="1"
android:layout_height="fill_parent"
android:text="1"
/>
<Button android:id="@+id/calc2"
android:layout_weight="1"
android:layout_height="fill_parent"
android:text="2"
/>
<Button android:id="@+id/calc3"
android:layout_weight="1"
android:layout_height="fill_parent"
android:text="3"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="1"
android:weightSum="3"
>
<Button android:id="@+id/calc4"
android:layout_weight="1"
android:layout_height="fill_parent"
android:text="4"
/>
<Button android:id="@+id/calc5"
android:layout_weight="1"
android:layout_height="fill_parent"
android:text="5"
/>
<Button android:id="@+id/calc6"
android:layout_weight="1"
android:layout_height="fill_parent"
android:text="6"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="1"
android:weightSum="3"
>
<Button android:id="@+id/calc7"
android:layout_weight="1"
android:layout_height="fill_parent"
android:text="7"
/>
<Button android:id="@+id/calc8"
android:layout_weight="1"
android:layout_height="fill_parent"
android:text="8"
/>
<Button android:id="@+id/calc9"
android:layout_weight="1"
android:layout_height="fill_parent"
android:text="9"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="1"
android:weightSum="3"
>
<Button android:id="@+id/calc0"
android:layout_weight="2"
android:layout_height="fill_parent"
android:text="0"
/>
<Button android:id="@+id/calcdec"
android:layout_weight="1"
android:layout_height="fill_parent"
android:text="."
/>
</LinearLayout>
<Button android:id="@+id/total"
android:layout_weight="1"
android:layout_width="fill_parent"
android:text="Total"
/>
</LinearLayout>
</LinearLayout>
如果我没记错的话,线性布局需要
android:layout\u width
和android:layout\u height
尺寸
在您的XML中,有几个没有android:layout\u width
另外,下次发布您的Logcat输出。在这些情况下,它们比代码本身有用得多。这就是为什么我在这里只是猜测一个曾经发生在我身上的问题
//编辑:
如果它仍然崩溃,请尝试在所有小部件中包含这两个维度。我不记得是否只是所有小部件的布局都需要这两个维度。普赖斯,把航海日志给我们。:-) 那没什么大不了的。尝试隔离是您的代码还是您的开发工具。我为布局添加了XML代码。我没有涉及任何JAVA代码,但我也添加了它。为什么要编辑XML布局?!?求你了,不要那样去改变你的东西。。。我在看它,看能不能发现什么。。。如果这是解决方案,建议通过点击绿色勾号来接受答案。@David,因为他是SO的新手,而有人以6k+的声誉无知地告诉他这没用。Jim,你认为XML是问题所在是正确的,特别是因为你没有接触Java,所以在这个问题上发表文章肯定是有用的。在未来,我们大多数人希望看到的是Logcat输出。它将帮助你和其他人解决你应用程序中的大量错误。这似乎并没有解决问题。如何确切地使用Logcat查找错误。我对Eclipse、Android和这个网站都是新手。很抱歉给您带来不便。是的,除了特殊情况(在
表格行
内),所有小部件都需要尺寸。为所有小部件添加高度和宽度可以解决此问题。谢谢大家!!我注意到你说添加二维会破坏你的布局。事实是,我看到你正试图“加重”你的布局。尝试在要加权的维度上使用“包裹内容”而不是“填充父对象”。例如,在第一行按钮上,您希望高度标注为包裹内容,因为您试图在外线布局中对垂直标注进行加权。有些人在加权时也尝试将“0”作为维度。试着进行实验,看看哪些案例效果更好。
07-22 01:24:41.793: ERROR/AndroidRuntime(392): at android.app.Activity.setContentView(Activity.java:1647)
07-22 01:24:41.793: ERROR/AndroidRuntime(392): at com.nubs.tableposv0_1.TablePOSv0_1Activity.onCreate(TablePOSv0_1Activity.java:15)
07-22 01:24:41.793: ERROR/AndroidRuntime(392): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-22 01:24:41.793: ERROR/AndroidRuntime(392): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-22 01:24:41.793: ERROR/AndroidRuntime(392): ... 11 more
07-22 01:25:41.482: ERROR/DefContainer(224): Couldn't copy file: /data/local/tmp/TablePOSv0_1.apk