Question

Now I've searched for hours to find the failure in my code. I'm getting a NPE in my code.

It's something with the ImageButton and the setOnClickListener.

Here the Stack Trace:

04-01 23:37:43.912: W/asset(6416):      Copying FileAsset 0x77cecd20 (zip:/data/app/net.test.apk:/resources.arsc) to buffer size 7000 to make it aligned.
04-01 23:37:43.962: W/dalvikvm(6416):       threadid=1: thread exiting with uncaught exception (group=0x416736f8)
04-01 23:37:43.972: E/AndroidRuntime(6416):     FATAL EXCEPTION: main
04-01 23:37:43.972: E/AndroidRuntime(6416):     Process: net.test, PID: 6416
04-01 23:37:43.972: E/AndroidRuntime(6416):  java.lang.RuntimeException: Unable to start activity ComponentInfo{net.test/net.test.MainActivity}: java.lang.NullPointerException
04-01 23:37:43.972: E/AndroidRuntime(6416):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
04-01 23:37:43.972: E/AndroidRuntime(6416):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
04-01 23:37:43.972: E/AndroidRuntime(6416):     at android.app.ActivityThread.access$800(ActivityThread.java)
04-01 23:37:43.972: E/AndroidRuntime(6416):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
04-01 23:37:43.972: E/AndroidRuntime(6416):     at android.os.Handler.dispatchMessage(Handler.java)
04-01 23:37:43.972: E/AndroidRuntime(6416):     at android.os.Looper.loop(Looper.java)
04-01 23:37:43.972: E/AndroidRuntime(6416):     at android.app.ActivityThread.main(ActivityThread.java)
04-01 23:37:43.972: E/AndroidRuntime(6416):     at java.lang.reflect.Method.invokeNative(Native Method)
04-01 23:37:43.972: E/AndroidRuntime(6416):     at java.lang.reflect.Method.invoke(Method.java)
04-01 23:37:43.972: E/AndroidRuntime(6416):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
04-01 23:37:43.972: E/AndroidRuntime(6416):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
04-01 23:37:43.972: E/AndroidRuntime(6416):     at dalvik.system.NativeStart.main(Native Method)
04-01 23:37:43.972: E/AndroidRuntime(6416):     Caused by: java.lang.NullPointerException
04-01 23:37:43.972: E/AndroidRuntime(6416):     at net.test.MainActivity.onCreate(MainActivity.java:24)
04-01 23:37:43.972: E/AndroidRuntime(6416):     at android.app.Activity.performCreate(Activity.java)
04-01 23:37:43.972: E/AndroidRuntime(6416):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)

and here the .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:background="@drawable/main_back"
android:gravity="center_horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="net.ringmybuddy.MainActivity$PlaceholderFragment" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="140dp"
    android:contentDescription="@string/button_step1"
    android:src="@drawable/start_image_step1" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_below="@+id/imageView1"
    android:contentDescription="@string/button_step2"
    android:src="@drawable/start_image_step2" />

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView2"
    android:layout_below="@+id/imageView2"
    android:contentDescription="@string/button_step3"
    android:src="@drawable/start_image_step3" />

<ImageView
    android:id="@+id/imageView4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView3"
    android:layout_below="@+id/imageView3"
    android:layout_marginTop="40dp"
    android:contentDescription="@string/start_rmb_schriftzug"
    android:src="@drawable/start_rmb_schriftzug" />

<ImageButton
    android:id="@+id/einrichten"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageView4"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="0dp"
    android:contentDescription="@string/button_einrichten"
    android:src="@drawable/start_button_einrichten" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="55dp"
    android:gravity="center_horizontal"
    android:text="@string/startroutine_einleitung"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#ffffff"
    android:textSize="15sp"
    android:textStyle="normal"
    android:typeface="normal" />

<ImageView
    android:id="@+id/imageView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/newtest"
    android:layout_marginBottom="17dp"
    android:layout_marginLeft="11dp"
    android:layout_toRightOf="@+id/newtest"
    android:contentDescription="@string/pfeil_2arrows"
    android:src="@drawable/start_image_arrows" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="0dp"
    android:text="@string/copyright"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#bcb081"
    android:textSize="12sp" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="-5dp"
    android:text="@string/startroutine_ueberschrift"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#ffffff"
    android:textSize="18sp"
    android:textStyle="bold"
    android:typeface="normal" />

</RelativeLayout>

the MainActivity:

package net.test;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
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.ImageButton;

public class MainActivity extends Activity implements View.OnClickListener{

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


    ImageButton einrichten = (ImageButton)findViewById(R.id.einrichten);

    einrichten.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v) {
                Intent in = new Intent
                        (
                                MainActivity.this,
                                EinstellungenMainActivityOn.class

                        );

                startActivity(in);
                }

    });

    if (savedInstanceState == null) {
        getFragmentManager().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.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;
    }
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

}

Yeah i know it's in line 24 of the MainActivity but i don't know how to fix that. I've tried everything and nothing had worked for me. I hope you guys can help me. Many thanks in advance!

Edit: Sorry guys I've forgotten to add the MainActivity.

Was it helpful?

Solution

you don't have einrichten id in activity_main , this xml that you post for us is for your fragment that is fragment_main, so you must have one ImageButton with einrichten id in activity_main or handle click on fragment class.

I write code for changing fragment for you,

so your code in fragment must be:

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

    ImageButton einrichten = (ImageButton)rootView.findViewById(R.id.einrichten);

    einrichten.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v) {
            Intent in = new Intent
                    (
                            getActivity(),
                            EinstellungenMainActivityOn.class

                    );

            startActivity(in);
            }

});

    return rootView;
}

and in MainActivity:

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


    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top