程序强制关闭

时间:2011-08-05 09:13:23

标签: android manifest

这是我的PlayerActivity:

 package player.org;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.MediaController;

import android.app.Activity;
import android.os.Bundle;

public class PlayerActivity extends Activity {
    /** Called when the activity is first created. */
    private MediaView media=new MediaView(this);
    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(media);
    }
}

这是我的MediaView:

    package player.org;
import android.app.Activity;
import android.view.*;
import android.widget.ImageView;
import android.content.*;
import android.content.*;
import android.graphics.Color;

    class MediaView extends View
            {
                public MediaView(Context context)
                    {
                        super(context);
                        setBackgroundColor(Color.WHITE);
                    }





            }

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="player.org"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity 
        android:name=".MediaView"
        android:label="@string/MediaView_title">

        </activity>
        <activity android:name=".PlayerActivity"
                  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>

但是当我启动应用程序时,它会强行关闭。

你有什么建议吗? 谢谢你的回答

1 个答案:

答案 0 :(得分:1)

在活动开始之前,您正在撰写此行private MediaView media=new MediaView(this);。所以'this'关键字强行关闭您的应用

请像这样使用

 public class PlayerActivity extends Activity {
    /** Called when the activity is first created. */
    private MediaView media;
    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(media);
      media=new MediaView(this);
    }
}