문제

I got problem with my first Android application. Just after running, my app crashes in emulator with "Unfortunately has stopped." message. I checked many threads on Stack and I read pretty much of official documentation and I really do not know what's a cause...

I use AVD with Android 4.4.2, API Level 19. CPU/ABI - ARM. Use Host GPU - checked (without this one emulator works very slowly).

My app should play a song after pressing the button. I would be really grateful for any ideas. Cheers.

Java file:

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;
import android.media.MediaPlayer;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

Button one = (Button) findViewById(R.id.button1);
final MediaPlayer mediaPlayer1 = MediaPlayer.create(this, R.raw.elements6);
one.setOnClickListener(new View.OnClickListener() {
    @Override
        public void onClick(View v) {
            mediaPlayer1.start();
        }
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}

}

And XML file:

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

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

</LinearLayout>

Errors:

05-14 17:29:21.740: D/AndroidRuntime(1145): Shutting down VM
05-14 17:29:21.740: W/dalvikvm(1145): threadid=1: thread exiting with uncaught exception (group=0xb2abcba8)
05-14 17:29:21.750: E/AndroidRuntime(1145): FATAL EXCEPTION: main
05-14 17:29:21.750: E/AndroidRuntime(1145): Process: com.example.beatzlooper, PID: 1145
05-14 17:29:21.750: E/AndroidRuntime(1145): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.beatzlooper/com.example.beatzlooper.MainActivity}: java.lang.NullPointerException
05-14 17:29:21.750: E/AndroidRuntime(1145):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-14 17:29:21.750: E/AndroidRuntime(1145):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-14 17:29:21.750: E/AndroidRuntime(1145):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-14 17:29:21.750: E/AndroidRuntime(1145):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-14 17:29:21.750: E/AndroidRuntime(1145):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-14 17:29:21.750: E/AndroidRuntime(1145):     at android.os.Looper.loop(Looper.java:136)
05-14 17:29:21.750: E/AndroidRuntime(1145):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-14 17:29:21.750: E/AndroidRuntime(1145):     at java.lang.reflect.Method.invokeNative(Native Method)
05-14 17:29:21.750: E/AndroidRuntime(1145):     at java.lang.reflect.Method.invoke(Method.java:515)
05-14 17:29:21.750: E/AndroidRuntime(1145):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-14 17:29:21.750: E/AndroidRuntime(1145):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-14 17:29:21.750: E/AndroidRuntime(1145):     at dalvik.system.NativeStart.main(Native Method)
05-14 17:29:21.750: E/AndroidRuntime(1145): Caused by: java.lang.NullPointerException
05-14 17:29:21.750: E/AndroidRuntime(1145):     at com.example.beatzlooper.MainActivity.onCreate(MainActivity.java:31)
05-14 17:29:21.750: E/AndroidRuntime(1145):     at android.app.Activity.performCreate(Activity.java:5231)
05-14 17:29:21.750: E/AndroidRuntime(1145):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-14 17:29:21.750: E/AndroidRuntime(1145):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-14 17:29:21.750: E/AndroidRuntime(1145):     ... 11 more
도움이 되었습니까?

해결책

You are working with fragments. The Fragment cannot be added to the R.id.container View because it doesn't exists.

Just delete the whole if-clause in the onCreate() and also delete the Placeholder Class you don't need it actually.

다른 팁

Comment this code

/*if (savedInstanceState == null) {
    getSupportFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment())
            .commit();
}*/
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top