不幸的是,App已停止在模拟器中工作

时间:2014-09-09 22:20:51

标签: android android-intent android-emulator

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

  

不幸的是,App已停止

我的应用程序是具有简单功能的音乐播放器应用程序。

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

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

  

09-10 16:01:28.460:E / AndroidRuntime(1200):引起:java.lang.InstantiationException:无法实例化类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_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>

代码 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 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

2 个答案:

答案 0 :(得分:1)

java.lang.InstantiationException: can't instantiate class com.example.musicplayer.MainActivity

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

答案 1 :(得分:0)

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

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