【问题标题】:Android layout xml file not runningAndroid布局xml文件未运行
【发布时间】:2012-07-16 00:41:45
【问题描述】:

下面是布局main.xml文件的代码

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button" />

</LinearLayout>

这是活动文件

package alvisoft.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

但是当我使用 android SDK 在 eclipse 上运行它时,它显示空白屏幕而不是显示按钮和 textview。我是绝对的初学者,不知道有什么问题请帮助我!

【问题讨论】:

  • 你的Activity在哪里,这里也加Activity代码
  • “当我在 Eclipse 上运行时”是什么意思?布局文件本身不是可执行文件,通常通过在onCreate(...)onCreate(...) 方法中调用setContentView(R.layout.main); 之类的东西来在运行时“膨胀”Activity
  • 您在 Eclipse 或运行时的图形布局中看到空白屏幕?确定您在顶部有 标记?
  • @biegleus 在添加它时会生成一个错误,提示“XML 声明中的版本后面的值必须被引用字符串”
  • 我又上传了活动文件检查

标签: android eclipse


【解决方案1】:

我也会像你一样犯错。您可以按如下方式修复此错误: - 在选项卡 Graphic_layout 上,您将布局 LinearLayout(左侧)拖放到界面中。 - 然后在 main.xml 选项卡上,您在此 LinearLayout 中创建了新代码。此错误已修复。

【讨论】:

  • 我在启动项目时默认以相同的方式进行操作,有一个相对布局我删除了它,然后拖放线性布局在此之后我拖放一个文本视图而不是 main.xml 文件我将 android:text 更改为 @string/button 我在 res/values 中声明了字符串,但我的问题尚未解决,请帮助我
【解决方案2】:

我想你忘了在AndroidManifest.xml 中添加你的活动。

像这样更新您的清单文件。

<?xml version="1.0" encoding="utf-8"?>
<manifest package="app.sample" android:versionCode="1"
    android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">

    <application android:icon="@drawable/icon" android:label="@string/app_name"
        android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen">

        <activity android:name=".MainActivity" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


    </application>

</manifest> 

我试过你的代码。它工作正常。但是R.menu.main 文件丢失了。我删除了选项菜单并尝试了

【讨论】:

    猜你喜欢
    • 2022-08-04
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    相关资源
    最近更新 更多