致命的例外 - 不幸的是App已经停止工作了

时间:2014-09-12 20:42:39

标签: android android-emulator fatal-error crash

我的应用程序在模拟器中崩溃。我看了logCat,发现有FATAL EXCEPTION,但我不知道如何解决这个问题。我读了类似的问题但是解决方案并没有解决我的问题。请帮助我。我的代码没有出错。

logcat的

09-12 16:25:41.390: E/AndroidRuntime(1270): FATAL EXCEPTION: main
09-12 16:25:41.390: E/AndroidRuntime(1270): Process: com.example.hareemaudio, PID: 1270
09-12 16:25:41.390: E/AndroidRuntime(1270): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hareemaudio/com.example.hareemaudio.MainActivity}: java.lang.NullPointerException
09-12 16:25:41.390: E/AndroidRuntime(1270):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
09-12 16:25:41.390: E/AndroidRuntime(1270):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
09-12 16:25:41.390: E/AndroidRuntime(1270):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
09-12 16:25:41.390: E/AndroidRuntime(1270):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
09-12 16:25:41.390: E/AndroidRuntime(1270):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-12 16:25:41.390: E/AndroidRuntime(1270):     at android.os.Looper.loop(Looper.java:137)
09-12 16:25:41.390: E/AndroidRuntime(1270):     at android.app.ActivityThread.main(ActivityThread.java:4998)
09-12 16:25:41.390: E/AndroidRuntime(1270):     at java.lang.reflect.Method.invokeNative(Native Method)
09-12 16:25:41.390: E/AndroidRuntime(1270):     at java.lang.reflect.Method.invoke(Method.java:515)
09-12 16:25:41.390: E/AndroidRuntime(1270):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
09-12 16:25:41.390: E/AndroidRuntime(1270):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
09-12 16:25:41.390: E/AndroidRuntime(1270):     at dalvik.system.NativeStart.main(Native Method)
09-12 16:25:41.390: E/AndroidRuntime(1270): Caused by: java.lang.NullPointerException
09-12 16:25:41.390: E/AndroidRuntime(1270):     at com.example.hareemaudio.MainActivity.getInit(MainActivity.java:42)
09-12 16:25:41.390: E/AndroidRuntime(1270):     at com.example.hareemaudio.MainActivity.onCreate(MainActivity.java:30)
09-12 16:25:41.390: E/AndroidRuntime(1270):     at android.app.Activity.performCreate(Activity.java:5243)
09-12 16:25:41.390: E/AndroidRuntime(1270):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-12 16:25:41.390: E/AndroidRuntime(1270):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
09-12 16:25:41.390: E/AndroidRuntime(1270):     ... 11 more

MainActivity

package com.example.hareemaudio;

import android.app.Activity;
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 class MainActivity extends Activity implements OnClickListener {
    SeekBar seek_bar;
    ImageButton play_button, pause_button;
    MediaPlayer player;
    TextView text_shown;
    Handler seekHandler = new Handler();

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

        //btnPlay.setClickable(true);
        //btnPlay.setEnabled(true);

        getInit();
        seekUpdation();
    }

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

    }

    Runnable run = new Runnable() {

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

    public void seekUpdation() {

        seek_bar.setProgress(player.getCurrentPosition());
        seekHandler.postDelayed(run, 1000);
    }

    /** yaha pe abi play/pause button k enable disable ka function lagaugi */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.play_button:

            /*buttonPlay.setEnabled(false);
            buttonPause.setEnabled(true);/*

                    //or

            /*View button = findViewById(R.id.btnPlay);
               button.setVisibility(View.GONE);
             * */
            //text_shown.setText("Playing...");



            player.start(); // Start playing Audio
            break;
        case R.id.pause_button:
            player.pause();  //Pause Audio
            //text_shown.setText("Paused...");

        }

    }
}

XML

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

    <!-- 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_marginTop="10dp"
            android:layout_weight="1"
            android:paddingLeft="10dp"
            android:text="1"
            android:textColor="#04b3d2"
            android:textSize="16dp"
            android:textStyle="bold" />

        <!-- Playlist button -->

        <ImageButton
            android:id="@+id/btnPlaylist"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:background="@null"
            android:src="@drawable/btn_playlist" />
    </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="50dp" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="200dp"
            android:layout_weight="1.04"
            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:gravity="center_vertical"
            android:orientation="horizontal"
            android:paddingLeft="10dp"
            android:paddingRight="10dp" >

            <!-- Previous Button -->
            <!-- Backward Button -->

            <ImageButton
                android:id="@+id/btnBackward"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"

                android:src="@drawable/btn_backward" />
            <!-- Play Button -->

            <ImageButton
                android:id="@+id/pause_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"
                android:clickable="false"
                android:src="@drawable/btn_pause" />

            <ImageButton
                android:id="@+id/play_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"
                 android:layout_toLeftOf="@+id/btnBackward"
                  android:layout_toRightOf="@+id/btnForward"
                android:src="@drawable/btn_play"
                />

            <!-- 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" />

            <!-- Next Button -->

        </LinearLayout>
    </LinearLayout>

    <!-- SABAK Progress Bar/Seek bar -->


    <!-- Timer Display -->


    <!-- Repeat / Shuffle buttons -->
    <!-- Repeat / Shuffle buttons -->

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/player_footer_bg"
        android:gravity="center"
        android:orientation="vertical" >

        <!-- Link to recordsabak Screen -->

        <SeekBar
            android:id="@+id/audioProgressBar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_weight="1"
            android:paddingLeft="6dp"
            android:paddingRight="6dp"
            android:paddingTop="10dp"
            android:progressDrawable="@drawable/seekbar_progress"
            android:thumb="@drawable/seek_handler" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/timerDisplay"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/AudioThumbnail" >
    </LinearLayout>

    <ImageButton
        android:id="@+id/imageButton10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linearLayout1"
        android:layout_marginRight="33dp"
        android:layout_toLeftOf="@+id/imageButton2"
        android:background="@null"
        android:src="@drawable/mic5" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linearLayout1"
        android:layout_alignParentRight="true"
        android:layout_marginRight="104dp"
        android:background="@null"
        android:src="@drawable/btn_shuffle" />

</RelativeLayout>

清单

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

2 个答案:

答案 0 :(得分:1)

我可以看到的一件事是你在R.id.seek_bar java文件中使用了getInit()。但是在xml文件中,此名称没有android:id。那可能会返回null。尝试将其更改为R.id.audioProgressBar

答案 1 :(得分:1)

在您的xml中,{J}代码中audioProgressBar的{​​{1}} seek_barseek_bar还是seek_bar = (SeekBar) findViewById(R.id.seek_bar);

变化:

seek_bar = (SeekBar) findViewById(R.id.audioProgressBar);

{{1}}