Koen Hilarius Koen Hilarius - 7 months ago 15
Java Question

The application has stopped unexpectedly, solutions?

Didn't change any code that I know of, worked completely fine yesterday.. Solutions anyone???
I have more activity's with codes, but those are all fine I think, so not gonna copy paste all of them.

If anyone has some ideas or solutions to this problem, help would be appreciated.

Main code :

package com.example.rodekruis;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener{

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

findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
findViewById(R.id.button5).setOnClickListener(this);
findViewById(R.id.button6).setOnClickListener(this);
findViewById(R.id.button7).setOnClickListener(this);
findViewById(R.id.button8).setOnClickListener(this);
findViewById(R.id.button9).setOnClickListener(this);
findViewById(R.id.button10).setOnClickListener(this);
}

@Override
public void onClick(View v) {
Intent intent = null;
switch (v.getId()) {

case R.id.button10:
intent = new Intent(MainActivity.this, AfspraakActivity.class);
break;
case R.id.button8:
intent = new Intent(MainActivity.this, BezoekActivity.class);
break;
case R.id.button9:
intent = new Intent(MainActivity.this, ContactActivity.class);
break;
case R.id.button3:
intent = new Intent(MainActivity.this, MeningActivity.class);
break;
case R.id.button4:
intent = new Intent(MainActivity.this, RouteActivity.class);
break;
case R.id.button1:
intent = new Intent(MainActivity.this, SpecialistenActivity.class);
break;
case R.id.button5:
intent = new Intent(MainActivity.this, BWCActivity.class);
break;
case R.id.button6:
intent = new Intent(MainActivity.this, AgendaActivity.class);
break;
case R.id.button7:
intent = new Intent(MainActivity.this, NieuwsActivity.class);
break;
}
startActivity(intent);
}
}


activity_main code:

<LinearLayout 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"
tools:context="com.example.rodekruis.MainActivity">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.rodekruis.MainActivity">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:src="@drawable/rkz_logo"
android:layout_gravity="center" />


<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="20dp"
android:orientation="horizontal">

<Button
android:id="@+id/button10"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignTop="@+id/button8"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text=" Afspraak maken" />


<Button
android:id="@+id/button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text=" Bezoek tijden" />

<Button
android:id="@+id/button9"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:text=" Contact" />

</LinearLayout>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:orientation="horizontal">


<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text=" Geef je mening!" />


<Button
android:id="@+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text=" Route begeleiding" />

<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_alignLeft="@+id/button5"
android:layout_below="@+id/button8"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:text="Specialisten" />

</LinearLayout>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">

<Button
android:id="@+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="1.03"
android:text="Brandwonden centrum" />


<Button
android:id="@+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text="Agenda" />


<Button
android:id="@+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:text="Nieuws" />
</LinearLayout>

</LinearLayout>
</LinearLayout>


Manifest code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rodekruis"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
<activity
android:name=".BezoekActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.rodekruis.BezoekActivity" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".AfspraakActivity"
android:label="@string/title_activity_afspraak" >
</activity>
<activity
android:name=".ContactActivity"
android:label="@string/title_activity_contact" >
</activity>
<activity
android:name=".MeningActivity"
android:label="@string/title_activity_mening" >
</activity>
<activity
android:name=".RouteActivity"
android:label="@string/title_activity_route" >
</activity>
<activity
android:name=".SpecialistenActivity"
android:label="@string/title_activity_specialisten" >
</activity>
<activity
android:name=".BWCActivity"
android:label="@string/title_activity_bwc" >
</activity>
<activity
android:name=".AgendaActivity"
android:label="@string/title_activity_agenda" >
</activity>
<activity
android:name=".NieuwsActivity"
android:label="@string/title_activity_info" >
</activity>
</application>

</manifest>


Logcat

0

4-19 11:34:58.608: D/AndroidRuntime(333): Shutting down VM
04-19 11:34:58.608: W/dalvikvm(333): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-19 11:34:58.628: E/AndroidRuntime(333): FATAL EXCEPTION: main
04-19 11:34:58.628: E/AndroidRuntime(333): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rodekruis/com.example.rodekruis.MainActivity}: java.lang.NullPointerException
04-19 11:34:58.628: E/AndroidRuntime(333): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-19 11:34:58.628: E/AndroidRuntime(333): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-19 11:34:58.628: E/AndroidRuntime(333): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-19 11:34:58.628: E/AndroidRuntime(333): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-19 11:34:58.628: E/AndroidRuntime(333): at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 11:34:58.628: E/AndroidRuntime(333): at android.os.Looper.loop(Looper.java:123)
04-19 11:34:58.628: E/AndroidRuntime(333): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-19 11:34:58.628: E/AndroidRuntime(333): at java.lang.reflect.Method.invokeNative(Native Method)
04-19 11:34:58.628: E/AndroidRuntime(333): at java.lang.reflect.Method.invoke(Method.java:507)
04-19 11:34:58.628: E/AndroidRuntime(333): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-19 11:34:58.628: E/AndroidRuntime(333): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-19 11:34:58.628: E/AndroidRuntime(333): at dalvik.system.NativeStart.main(Native Method)
04-19 11:34:58.628: E/AndroidRuntime(333): Caused by: java.lang.NullPointerException
04-19 11:34:58.628: E/AndroidRuntime(333): at com.example.rodekruis.MainActivity.onCreate(MainActivity.java:21)
04-19 11:34:58.628: E/AndroidRuntime(333): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-19 11:34:58.628: E/AndroidRuntime(333): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-19 11:34:58.628: E/AndroidRuntime(333): ... 11 more

Answer

Your layout does not seem to have a button with id button2 in it and the app crashes here:

findViewById(R.id.button2).setOnClickListener(this);

because findViewById(R.id.button2) returns null.

Comments