I added a second activity to my perfectly working application and after that it crashes already when running. I though I did all necessary things to make it works, like cleaning project, adding reference into manifest file etc., but it still doesn't work. New activity should start after choosing imageButtonInfo. Any ideas what could be wrong?
MainActivity.java:
package com.example.beatzlooper;
import java.io.IOException;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.os.Build;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
public class MainActivity extends ActionBarActivity implements OnClickListener{
/* Declarations */
MediaPlayer mp = new MediaPlayer();
Beat beatOne = new Beat("Unknown", "1 asdasfsdfsdfsdfsfdad", R.raw.elements6); // (author, title, id)
Beat beatTwo = new Beat("Unknown", "2 adasdavvvzzzzfff", R.raw.beat);
TextView labelView = (TextView) findViewById(R.id.labelView);
Intent intent = new Intent(MainActivity.this, InfoActivity.class);
/* ----------------------------------------------------------- */
@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();
// }
/* Buttons */
ImageButton btnStop = (ImageButton) findViewById(R.id.imageButtonStop);
btnStop.setOnClickListener(this);
ImageButton btnInfo = (ImageButton) findViewById(R.id.imageButtonInfo);
btnInfo.setOnClickListener(this);
ImageButton btnExit = (ImageButton) findViewById(R.id.imageButtonExit);
btnExit.setOnClickListener(this);
Button btnOne = (Button) findViewById(R.id.button1);
btnOne.setOnClickListener(this);
Button btnTwo = (Button) findViewById(R.id.button2);
btnTwo.setOnClickListener(this);
/* ----------------------------------------------------------- */
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
mp.stop();
mp.release();
mp = null;
mp = MediaPlayer.create(this, beatOne.id());
mp.setLooping(true);
labelView.setText(1 + ". " + beatTwo.author() + " - " + beatOne.title());
labelView.setTextColor(Color.parseColor("#FFFFFF"));
mp.start();
break;
case R.id.button2:
mp.stop();
mp.release();
mp = null;
mp = MediaPlayer.create(this, beatTwo.id());
mp.setLooping(true);
labelView.setText(2 + ". " + beatTwo.author() + " - " + beatTwo.title());
labelView.setTextColor(Color.parseColor("#FFFFFF"));
mp.start();
break;
case R.id.imageButtonStop:
mp.stop();
mp.release();
mp = null;
break;
case R.id.imageButtonInfo:
mp.stop();
mp.release();
mp = null;
MainActivity.this.startActivity(intent);
break;
case R.id.imageButtonExit:
android.os.Process.killProcess(android.os.Process.myPid());
break;
default:
return;
}
}
@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;
}
}
}
InfoActivity.java:
package com.example.beatzlooper;
import com.example.beatzlooper.R;
import com.example.beatzlooper.R.id;
import com.example.beatzlooper.R.layout;
import com.example.beatzlooper.R.menu;
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.os.Build;
public class InfoActivity extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
// if (savedInstanceState == null) {
// getSupportFragmentManager().beginTransaction()
// .add(R.id.container, new PlaceholderFragment()).commit();
// }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.info, 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_info, container,
false);
return rootView;
}
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.beatzlooper"
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=".MainActivity"
android:configChanges="orientation|screenSize"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".InfoActivity"
android:configChanges="orientation|screenSize"
android:label="@string/app_name_info" >
</activity>
</application>
</manifest>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="@drawable/bandanabackground"
android:columnCount="2"
android:orientation="horizontal" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignParentRight="true"
android:layout_marginRight="35dp"
android:text="2" />
<ImageButton
android:id="@+id/imageButtonInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp"
android:background="@drawable/buttonstyle"
android:padding="10dp"
android:src="@drawable/info_invert" />
<ImageButton
android:id="@+id/imageButtonExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_alignTop="@+id/imageButtonInfo"
android:background="@drawable/buttonstyle"
android:padding="10dp"
android:src="@drawable/exit_invert" />
<ImageButton
android:id="@+id/imageButtonStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_alignTop="@+id/imageButtonInfo"
android:background="@drawable/buttonstyle"
android:padding="10dp"
android:src="@drawable/stop_invert" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/labelView"
android:layout_marginLeft="37dp"
android:layout_marginTop="42dp"
android:text="1" />
<TextView
android:id="@+id/labelView"
android:layout_width="16dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/imageButtonExit"
android:layout_marginTop="39dp"
android:gravity="center" />
</RelativeLayout>
activity_info.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</RelativeLayout>
LogCat:
05-24 17:53:24.309: D/AndroidRuntime(18837): Shutting down VM
05-24 17:53:24.309: W/dalvikvm(18837): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
05-24 17:53:24.309: E/AndroidRuntime(18837): FATAL EXCEPTION: main
05-24 17:53:24.309: E/AndroidRuntime(18837): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.beatzlooper/com.example.beatzlooper.MainActivity}: java.lang.NullPointerException
05-24 17:53:24.309: E/AndroidRuntime(18837): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1891)
05-24 17:53:24.309: E/AndroidRuntime(18837): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
05-24 17:53:24.309: E/AndroidRuntime(18837): at android.app.ActivityThread.access$600(ActivityThread.java:127)
05-24 17:53:24.309: E/AndroidRuntime(18837): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
05-24 17:53:24.309: E/AndroidRuntime(18837): at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 17:53:24.309: E/AndroidRuntime(18837): at android.os.Looper.loop(Looper.java:137)
05-24 17:53:24.309: E/AndroidRuntime(18837): at android.app.ActivityThread.main(ActivityThread.java:4441)
05-24 17:53:24.309: E/AndroidRuntime(18837): at java.lang.reflect.Method.invokeNative(Native Method)
05-24 17:53:24.309: E/AndroidRuntime(18837): at java.lang.reflect.Method.invoke(Method.java:511)
05-24 17:53:24.309: E/AndroidRuntime(18837): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-24 17:53:24.309: E/AndroidRuntime(18837): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-24 17:53:24.309: E/AndroidRuntime(18837): at dalvik.system.NativeStart.main(Native Method)
05-24 17:53:24.309: E/AndroidRuntime(18837): Caused by: java.lang.NullPointerException
05-24 17:53:24.309: E/AndroidRuntime(18837): at android.app.Activity.findViewById(Activity.java:1794)
05-24 17:53:24.309: E/AndroidRuntime(18837): at com.example.beatzlooper.MainActivity.<init>(MainActivity.java:30)
05-24 17:53:24.309: E/AndroidRuntime(18837): at java.lang.Class.newInstanceImpl(Native Method)
05-24 17:53:24.309: E/AndroidRuntime(18837): at java.lang.Class.newInstance(Class.java:1319)
05-24 17:53:24.309: E/AndroidRuntime(18837): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
05-24 17:53:24.309: E/AndroidRuntime(18837): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1882)
05-24 17:53:24.309: E/AndroidRuntime(18837): ... 11 more