程序强制关闭

发布于 2024-11-28 02:12:33 字数 2127 浏览 0 评论 0原文

这是我的 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>

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

您有什么建议吗? 感谢您的回答

Here is my 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);
    }
}

here is my 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);
                    }





            }

and here is my manifest:

<?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>

But when I Launch the app., It force closes.

Do you have any suggestions?
Thanks for your answers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑾夏年华 2024-12-05 02:12:33

您正在编写此行 private MediaView media=new MediaView(this);,在 Activity 启动之前。因此 '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);
    }
}

You are writing this line private MediaView media=new MediaView(this);,before the Activity is initiated.So 'this' keyword is force closing your application

Please use like 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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文