【问题标题】:App is not opening , though no errors..!应用程序没有打开,虽然没有错误..!
【发布时间】:2014-06-27 11:42:45
【问题描述】:

我尝试制作一个简单的应用程序。 这是我在 android 中的第一个应用程序,但我对 java 并不陌生。 该应用程序只有一个自定义背景和主屏幕上播放的声音(歌曲)。 它构建成功。完全没有错误..!!! 我什至处理了警告..

但是这个应用程序不能在设备上运行(我不使用模拟器..!!) 它说,“不幸的是 [appname] 已停止”..

为什么????

这是我的java代码..:

package com.exmple.worth;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    MediaPlayer bfmv = MediaPlayer.create(MainActivity.this, R.raw.saf ) ;
    Button b1 ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1 = (Button) findViewById(R.id.b1) ;
        bfmv.start() ;

        b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                bfmv.stop() ;
            }
        });
    }

}

这是我的 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"
    tools:context="${packageName}.${activityClass}" 
    android:background="@drawable/qwe" >"

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="14dp"
        android:text="Pause" />

</RelativeLayout>

这里是 Android 清单:

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

    <uses-sdk
        android:minSdkVersion="13"
        android:targetSdkVersion="18" />

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

【问题讨论】:

  • 你的 logcat 应该显示它为什么崩溃了?
  • 发布包含崩溃时引发的异常的 LOGCAT
  • 你的 logcat 过滤了吗?
  • Logcat ..??先生,绝对没有错误......

标签: java android xml eclipse android-layout


【解决方案1】:

我试过你的代码。你实例化 bfmv 的方式是错误的。

MediaPlayer bfmv = MediaPlayer.create(MainActivity.this, R.raw.saf ) ;

应该是这样的:在外部类上只声明对象:

MediaPlayer bfmv;

然后在你的 oncreate 方法上实例化它:

bfmv = MediaPlayer.create(MainActivity.this, R.raw.saf ) ;

强制关闭的原因是在调用oncreate之前上下文MainActivity.this为null。

【讨论】:

  • @user2681440 欢迎您我尝试了您的代码。如果它对你有用,那么介意接受我的回答。 +1 真的很好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-27
  • 1970-01-01
相关资源
最近更新 更多