gpt4 book ai didi

android - 不幸的是,应用程序已停止在模拟器中运行

转载 作者:行者123 更新时间:2023-11-30 02:41:02 26 4
gpt4 key购买 nike

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

Unfortunately App has stopped

我的应用是功能简单的音乐播放器应用。

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

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

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

最佳答案

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

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

关于android - 不幸的是,应用程序已停止在模拟器中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25754465/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com