【问题标题】:Displays a black screen in my android application - in Eclipse在我的 android 应用程序中显示黑屏 - 在 Eclipse 中
【发布时间】:2014-04-27 19:08:09
【问题描述】:

我是 Android 编程的初学者,我遇到了一个大问题。 使用开发环境 Eclispe。创建秒表应用程序,但是 总是在我的应用程序中只显示黑屏。由于通过我的手机创建项目的开始是通过 Android ADB 连接的,因此只显示黑屏。我在这个网站上看了一些帖子,但我仍然不知道问题是什么。

我的 MainActivity.java 文件:

package cz.miegl.stopky;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView textTimer;
    private Button startButton;
    private Button pauseButton;
    private long startTime = 0L;
    private Handler myHandler = new Handler();
    long timeInMillies = 0L;
    long timeSwap = 0L;
    long finalTime = 0L;

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

        textTimer = (TextView) findViewById(R.id.textTimer);

        startButton = (Button) findViewById(R.id.btnStart);
        startButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                startTime = SystemClock.uptimeMillis();
                myHandler.postDelayed(updateTimerMethod, 0);

            }
        });

        pauseButton = (Button) findViewById(R.id.btnPause);
        pauseButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                timeSwap += timeInMillies;
                myHandler.removeCallbacks(updateTimerMethod);

            }
        });

    }

    private Runnable updateTimerMethod = new Runnable() {

        public void run() {
            timeInMillies = SystemClock.uptimeMillis() - startTime;
            finalTime = timeSwap + timeInMillies;

            int seconds = (int) (finalTime / 1000);
            int minutes = seconds / 60;
            seconds = seconds % 60;
            int milliseconds = (int) (finalTime % 1000);
            textTimer.setText("" + minutes + ":"
                    + String.format("%02d", seconds) + ":"
                    + String.format("%03d", milliseconds));
            myHandler.postDelayed(this, 0);
        }

    };

}

我的 activity_main.xml 文件:

<RelativeLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white" >
    <Button

    android:id="@+id/btnPause"
    android:layout_width="90dp"
    android:layout_marginLeft="20dp"
    android:layout_height="45dp"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/btnStart"
    android:text="Pauza" />

    <Button
    android:id="@+id/btnStart"
    android:layout_width="90dp"
    android:layout_height="45dp"

    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="68dp"
    android:text="Start" />

    <TextView
    android:id="@+id/textTimer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/btnPause"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="37dp"
    android:textSize="40sp"
    android:textColor="#000000"
    android:text="00:00:00" />

</RelativeLayout>

还有我的 AndroidManifest.xml 文件:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="cz.miegl.stopky.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>

请帮助我。我不知道该怎么办。谢谢!

请帮助我!!!谢谢!

【问题讨论】:

    标签: java android xml development-environment


    【解决方案1】:

    您的 activity_main.xml 文件中有一个框架布局。这是另一个视图的占位符,因此不显示任何内容。通常,当您使用片段时,您会使用此视图。但是,您没有使用片段。

    由于您是初学者,请将 fragment_main 中的全部内容复制到 activity_main.xml 文件中。

    【讨论】:

    • 谢谢。如何归档 fragment_main.xml 链接?
    • 我将 fragment_main.xml 的内容复制到了 activity_main.xml 但仍然没有。还是黑屏……怎么办?
    • 抱歉,我注意到您在代码中引用了 R.Layout.fragment_main,请将其更改为 R.layout.activity_main。您在 setContentView 中引用的视图将被夸大。
    • 我想给 fragment_main.xml 按钮等。我应该在哪里给?
    • 是的,我将代码路径更改为activity_main.xml,但仍然没有。我不知道该怎么办。
    【解决方案2】:

    现在看起来您正在将主要活动和主要片段混合在一起。我还是 Fragment 的新手,但我很确定在你的活动的 OnCreate 中,你想调用

    setContentView(R.id.activity_main)
    

    然后在activity_main.xml中,引用片段。

    查看片段编程指南http://developer.android.com/guide/topics/fundamentals/fragments.html

    【讨论】:

      猜你喜欢
      • 2016-08-06
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 2011-09-11
      • 1970-01-01
      • 2012-12-28
      • 1970-01-01
      • 2020-07-22
      相关资源
      最近更新 更多