不幸的是,应用程序已停止在模拟器中工作

Posted

技术标签:

【中文标题】不幸的是,应用程序已停止在模拟器中工作【英文标题】:Unfortunately App has stopped working In Emulator 【发布时间】:2014-11-03 10:56:19 【问题描述】:

我在模拟器中运行我的应用时遇到此错误:

不幸的是应用程序已停止

My App 是一款功能简单的音乐播放器应用。

所有其他应用都在同一个模拟器中完美运行。只有这个应用程序不工作。

查看日志后,我也看到了这个错误:

09-10 16:01:28.460: E/androidRuntime(1200): Caused by: java.lang.InstantiationException: can't instantiate class com.example.musicplayer.MainActivity

MainActivity.java的代码:

package com.example.musicplayer;

import com.example.musicplayer.R;

//import com.example.musicplayer.secondscreen;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
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.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

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

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getInit();
        seekUpdation();
        addListenerOnButton();
    

 //Function for Button press to change the screen

    public void addListenerOnButton() 

        final Context context = this;

        button = (ImageButton) findViewById(R.id.btnForward);

        button.setOnClickListener(new OnClickListener() 

            @Override
            public void onClick(View arg0) 

                Intent intent = new Intent(context, SecondScreen.class);
                            startActivity(intent);   

            

        );

    

    //function for play/pause Audio

    public void getInit() 
        seek_bar = (SeekBar) findViewById(R.id.seek_bar);
        play_button = (Button) findViewById(R.id.btnPlay);
        pause_button = (Button) findViewById(R.id.btnPause);
        //text_shown = (TextView) findViewById(R.id.text_shown);
        play_button.setOnClickListener(this);
        pause_button.setOnClickListener(this);
        player = MediaPlayer.create(this, R.raw.fat); //ye wo Audio file hai jo chalarae
        seek_bar.setMax(player.getDuration());

    

    Runnable run = new Runnable() 

        @Override
        public void run() 
            seekUpdation();
        
    ;

    // function for update seekbar

    public void seekUpdation() 
        seek_bar.setProgress(player.getCurrentPosition());
        seekHandler.postDelayed(run, 1000);
    

activity_main.xml的代码:

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

    <!-- Player Header -->
    <LinearLayout 
       android:id="@+id/player_header_bg"
        android:layout_
        android:layout_

        android:layout_alignParentTop="true"
        android:paddingLeft="5dp"
        android:paddingRight="5dp">

        <!-- Audio Title -->
        <TextView 
            android:id="@+id/audioTitle"
            android:layout_
            android:layout_
            android:layout_weight="1"
            android:textColor="#04b3d2"
            android:textSize="16dp"
            android:paddingLeft="10dp"
            android:textStyle="bold"
            android:text="1"
            android:layout_marginTop="10dp"/>

        <!-- Playlist button -->
        <ImageButton 
            android:id="@+id/btnPlaylist"
            android:layout_
            android:layout_
            android:src="@drawable/btn_playlist"
            android:background="@null"/>
    </LinearLayout>

    <!-- Sabak  Image -->

    <LinearLayout
        android:id="@+id/AudioThumbnail"
        android:layout_
        android:layout_

        android:gravity="center"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" >

        <ImageView
            android:layout_
            android:layout_
            android:src="@drawable/fatiha" />

    </LinearLayout>

    <!-- Player Footer -->
    <LinearLayout 
        android:id="@+id/player_footer_bg"
        android:layout_
        android:layout_
        android:layout_alignParentBottom="true"

        android:gravity="center">

        <!-- Player Buttons -->
        <LinearLayout 
            android:layout_
            android:layout_
            android:orientation="horizontal"
            android:gravity="center_vertical"

            android:paddingLeft="10dp"
            android:paddingRight="10dp">
            <!-- Previous Button -->
            <!-- Backward Button -->
            <ImageButton 
                android:id="@+id/btnBackward"
                android:src="@drawable/btn_backward"
                android:layout_
                android:layout_
                android:background="@null"/>

            <!-- Pause Button -->


            <ImageButton
                android:id="@+id/btnPause"
                android:layout_
                android:layout_
                android:background="@null"
                android:enabled="false"
                android:gravity="center"
                android:src="@drawable/btn_pause" />


<!-- Play Button -->            
<ImageButton
                android:id="@+id/btnPlay"
                android:layout_
                android:layout_
                android:background="@null"
                android:src="@drawable/btn_play" 
               android:gravity="center" />

  <!-- Forward Button -->

            <ImageButton
                android:id="@+id/btnForward"
                android:layout_
                android:layout_
                android:background="@null"
                android:src="@drawable/btn_forward" />
        </LinearLayout>
    </LinearLayout>

    <!-- SABAK Progress Bar/Seek bar -->
    <SeekBar
               android:id="@+id/seek_bar"
             android:layout_
             android:layout_
             android:layout_marginRight="20dp" 
             android:layout_marginLeft="20dp"
             android:layout_marginBottom="20dp"
             android:layout_above="@id/player_footer_bg"
             android:thumb="@drawable/seek_handler"
             android:progressDrawable="@drawable/seekbar_progress"
             android:paddingLeft="6dp"
             android:paddingRight="6dp"/>

    <LinearLayout
        android:layout_
        android:layout_
        android:layout_above="@+id/audioProgressBar"
        android:gravity="center" >

 </LinearLayout>

</RelativeLayout>

Manifest.xml 的代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.musicplayer"
    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="com.example.musicplayer.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>

LogCat

09-10 16:01:28.250: D/dalvikvm(1200): newInstance failed: p0 i0 [0 a1
09-10 16:01:28.250: D/AndroidRuntime(1200): Shutting down VM
09-10 16:01:28.260: W/dalvikvm(1200): threadid=1: thread exiting with uncaught exception (group=0xb3b0bb90)
09-10 16:01:28.460: E/AndroidRuntime(1200): FATAL EXCEPTION: main
09-10 16:01:28.460: E/AndroidRuntime(1200): Process: com.example.musicplayer, PID: 1200
09-10 16:01:28.460: E/AndroidRuntime(1200): java.lang.RuntimeException: Unable to instantiate activity ComponentInfocom.example.musicplayer/com.example.musicplayer.MainActivity: java.lang.InstantiationException: can't instantiate class com.example.musicplayer.MainActivity
09-10 16:01:28.460: E/AndroidRuntime(1200):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
09-10 16:01:28.460: E/AndroidRuntime(1200):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
09-10 16:01:28.460: E/AndroidRuntime(1200):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
09-10 16:01:28.460: E/AndroidRuntime(1200):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
09-10 16:01:28.460: E/AndroidRuntime(1200):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-10 16:01:28.460: E/AndroidRuntime(1200):     at android.os.Looper.loop(Looper.java:137)
09-10 16:01:28.460: E/AndroidRuntime(1200):     at android.app.ActivityThread.main(ActivityThread.java:4998)
09-10 16:01:28.460: E/AndroidRuntime(1200):     at java.lang.reflect.Method.invokeNative(Native Method)
09-10 16:01:28.460: E/AndroidRuntime(1200):     at java.lang.reflect.Method.invoke(Method.java:515)
09-10 16:01:28.460: E/AndroidRuntime(1200):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
09-10 16:01:28.460: E/AndroidRuntime(1200):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
09-10 16:01:28.460: E/AndroidRuntime(1200):     at dalvik.system.NativeStart.main(Native Method)
09-10 16:01:28.460: E/AndroidRuntime(1200): Caused by: java.lang.InstantiationException: can't instantiate class com.example.musicplayer.MainActivity
09-10 16:01:28.460: E/AndroidRuntime(1200):     at java.lang.Class.newInstanceImpl(Native Method)
09-10 16:01:28.460: E/AndroidRuntime(1200):     at java.lang.Class.newInstance(Class.java:1208)
09-10 16:01:28.460: E/AndroidRuntime(1200):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
09-10 16:01:28.460: E/AndroidRuntime(1200):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
09-10 16:01:28.460: E/AndroidRuntime(1200):     ... 11 more

【问题讨论】:

请显示您的堆栈跟踪。我们需要知道异常是什么。 如果没有您得到的异常的堆栈跟踪,我们将无法帮助您。从 logcat 添加完整的堆栈跟踪。 (PS:如果错误是关于在清单中声明活动,请检查下面的答案) 请看一下我刚刚发布的 Logcat 消息。 【参考方案1】:
java.lang.InstantiationException: can't instantiate class com.example.musicplayer.MainActivity

您的MainActivityabstract,并且无法实例化抽象类。从类声明中删除 abstract 关键字。

【讨论】:

【参考方案2】:

您正在尝试启动未在 Manifest 文件中声明的 SecondScreen 活动。

<activity android:name="com.example.musicplayer.SecondScreen" /> 

有关如何在活动之间切换和在它们之间传递数据的更多详细信息,您可以按照本教程进行操作: http://www.android-ios-tutorials.com/117/how-to-switch-between-different-activities-in-android/

编辑

活动MainActivity 不应该是抽象的;从声明中删除abstract

public class MainActivity extends Activity ...

【讨论】:

是的,您是对的。感谢您的回答:) 我现在在我的清单文件中添加了这一行。但问题仍然存在......:(请看一下我刚刚在这里发布的 logCat @Liza : 查看我的编辑,abstract 关键字应该被删除,activity 不应该是抽象的

以上是关于不幸的是,应用程序已停止在模拟器中工作的主要内容,如果未能解决你的问题,请参考以下文章

不幸的是,*app* 已停止 [重复]

不幸的是,应用程序已在某些 android 设备上停止

IBM Worklight - Android SDK - 模拟器部署问题 - 不幸的是,“应用程序”已停止

不幸的是,Apps已停止在某些Android设备上

Eclipse - 不幸的是应用程序已停止 - ClassNotFoundException:在路径上找不到类

Android 应用程序错误 - 不幸的是,您的应用程序已停止