简体   繁体   English

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

[英]Unfortunately App has stopped working In Emulator

I got this error while running my app in emulator: 在模拟器中运行我的应用程序时出现此错误:

Unfortunately App has stopped 不幸的是,应用程序已停止

My App is music player app with simple functionality. 我的应用程序是具有简单功能的音乐播放器应用程序。

All other apps are running perfectly in the same emulator. 所有其他应用程序都可以在同一模拟器中完美运行。 Only this app is not working. 只有此应用无法正常工作。

After looking at the log, I also see this error: 查看日志后,我还看到此错误:

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):由以下原因引起:java.lang.InstantiationException:无法实例化com.example.musicplayer.MainActivity类

Code for MainActivity.java : 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);
    }

Code for activity_main.xml : activity_main.xml的代码:

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

    <!-- Player Header -->
    <LinearLayout 
       android:id="@+id/player_header_bg"
        android:layout_width="fill_parent"
        android:layout_height="60dip"

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

        <!-- Audio Title -->
        <TextView 
            android:id="@+id/audioTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            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_width="wrap_content"
            android:layout_height="fill_parent"
            android:src="@drawable/btn_playlist"
            android:background="@null"/>
    </LinearLayout>

    <!-- Sabak  Image -->

    <LinearLayout
        android:id="@+id/AudioThumbnail"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

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

        <ImageView
            android:layout_width="292dp"
            android:layout_height="283dp"
            android:src="@drawable/fatiha" />

    </LinearLayout>

    <!-- Player Footer -->
    <LinearLayout 
        android:id="@+id/player_footer_bg"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"

        android:gravity="center">

        <!-- Player Buttons -->
        <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            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_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"/>

            <!-- Pause Button -->


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


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

  <!-- Forward Button -->

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

    <!-- SABAK Progress Bar/Seek bar -->
    <SeekBar
               android:id="@+id/seek_bar"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             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_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/audioProgressBar"
        android:gravity="center" >

 </LinearLayout>

</RelativeLayout>

Code For Manifest.xml : 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 : 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 ComponentInfo{com.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
java.lang.InstantiationException: can't instantiate class com.example.musicplayer.MainActivity

Your MainActivity is abstract and abstract classes cannot be instantiated. 您的MainActivityabstract ,抽象类无法实例化。 Remove the abstract keyword from the class declaration. 从类声明中删除abstract关键字。

You are trying to start the SecondScreen activity which is not declared on the Manifest File . 您正在尝试启动Manifest文件中未声明的SecondScreen活动。

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

For more details about how to switch between activities and pass data between them , you can follow this tutorial : http://www.android-ios-tutorials.com/117/how-to-switch-between-different-activities-in-android/ 有关如何在活动之间进行切换以及如何在活动之间传递数据的更多详细信息,可以按照以下教程进行操作: http : //www.android-ios-tutorials.com/117/how-to-switch-between-different-activities-in -Android /

EDIT : 编辑

The activity MainActivity should not be abstract ; 活动MainActivity不应是抽象的; remove the abstract from the declaration : 从声明中删除abstract

public class MainActivity extends Activity ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM