Android应用程序没有显示任何内容

时间:2014-06-24 21:14:51

标签: android xml android-layout

我尝试使用播放/暂停和基于本教程的滑块创建基本音频播放器:http://mrbool.com/how-to-play-audio-files-in-android-with-a-seekbar-feature-and-mediaplayer-class/28243

所有内容都可以正常编译,但是在模拟器或设备上的活动中都没有显示任何内容。设备api级别18和AVD api级别19

MainActivity.java

package com.ex.highline;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;


public class MainActivity extends Activity implements OnClickListener {
    SeekBar seek_bar;
    Button play_button, pause_button;
    MediaPlayer player;
    TextView text_shown;
    Handler seekHandler = new Handler();

public void onClick(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    seekUpdate();

    /* Initialize all views */
    seek_bar = (SeekBar) findViewById(R.id.seek_bar);

    play_button = (Button) findViewById(R.id.play_button);

    pause_button = (Button) findViewById(R.id.pause_button);

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

    play_button.setOnClickListener((android.view.View.OnClickListener) this);
    pause_button.setOnClickListener((android.view.View.OnClickListener) this);

    player = MediaPlayer.create(this, R.raw.money);

    seek_bar.setMax(player.getDuration());

}

Runnable run = new Runnable() {

    @Override
    public void run(){

        seekUpdate();
    }
};


public void seekUpdate() {

    seek_bar.setProgress(player.getCurrentPosition());
    seekHandler.postDelayed(run, 1000);
}

public void onClick(View view){
    switch (view.getId()){
    case R.id.play_button:
        text_shown.setText("Playing...");
        player.start();
        break;
    case R.id.pause_button:
        player.pause();
        text_shown.setText("Paused...");
    }
}



}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
    android:id="@+id/text_shown"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="120dp"
    android:text=""
    android:textSize="42sp" />

<SeekBar
    android:id="@+id/seek_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true" />

<Button
    android:id="@+id/pause_button"
    android:layout_width="120dp"
    android:layout_height="60dp"
    android:layout_alignBaseline="@+id/play_button"
    android:layout_alignBottom="@+id/play_button"
    android:layout_toRightOf="@+id/text_shown"
    android:text="@string/pause" />

<Button
    android:id="@+id/play_button"
    android:layout_width="120dp"
    android:layout_height="60dp"
    android:layout_alignRight="@+id/text_shown"
    android:layout_below="@+id/seek_bar"
    android:layout_marginTop="44dp"
    android:gravity="center"
    android:text="@string/play" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:2)

有两处变化:

1) OncLick ==&gt; onCreate

2)将seekUpdate();放在end of onCreate method(警告你有变奏曲)**

  public class MainActivity extends Activity implements OnClickListener {

    SeekBar seek_bar;
    Button play_button, pause_button;
    MediaPlayer player;
    TextView text_shown;
    Handler seekHandler = new Handler();

    public void onCreate(Bundle savedInstanceState) { // <== change here 

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    /* Initialize all views */
    seek_bar = (SeekBar) findViewById(R.id.seek_bar);

    play_button = (Button) findViewById(R.id.play_button);

    pause_button = (Button) findViewById(R.id.pause_button);

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

    play_button.setOnClickListener((android.view.View.OnClickListener) this);
    pause_button.setOnClickListener((android.view.View.OnClickListener) this);

    player = MediaPlayer.create(this, R.raw.test);

    seek_bar.setMax(player.getDuration());
    seekUpdate();  // <==== change here
}
// the rest of code

enter image description here

答案 1 :(得分:0)

在Android中,活动显示为响应意图。 当您点击手机中的Android App图标时,您正在启动android.intent.action.MAIN类别android.intent.category.LAUNCHER的动作,所以...如果您想查看活动,请确保您你的AndroidManifest.xml中有这样的东西:

<activity
        android:name=".MainActivity">

        <!-- intent filters -->
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

其中.MainActivity是您的活动的类名,应用包作为前缀。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ex.highline"
android:versionCode="1"
android:versionName="1.0" >

所以...应用程序包+活动主要是您要启动的活动完整类名(当您单击应用程序图标时)。