5
votes

I'm new in Android developing and I'm having some issues with creating a new Activity. I want to start a new Activity when a Button is clicked but the app crashes. Here is what is said in the console and in the logcat:

LogCat

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

activity_main

<pre><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.src.vicnote.MainActivity"
tools:ignore="MergeRootFrame" /><code>

fragment_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/MainScreen" android:layout_width="match_parent" android:layout_height="match_parent" 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="com.src.vicnote.MainActivity$PlaceholderFragment" > <Button android:id="@+id/buttonNew" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="New" /> </RelativeLayout>

Java code

http://pastebin.com/K8eAWD2L

7
it crashes right when it starts up?JRowan
its saying that your button is nullJRowan
Can you post your xml? It appears that buttonNew is not a valid id in activity_main.xmlcodeMagic
Post activity_main.xml filegunar
I'm trying to fix this ugly post, sorry for it.. Yes, JRowan, it crashes when it starts. Thank you for your replies :)user3132352

7 Answers

2
votes

from what it looks like is your setting the content view

setContentView(R.layout.activity_main);

but there is no button in activity_main.xml from what you have provided and it is showing a nullpointerexception because

newButton = (Button) findViewById(R.id.buttonNew);

is null because that actual button from the information you provided is in

fragment_main.xml

so you can try

setContentView(R.layout.fragment_main);
1
votes

Check your Manifest.xml and make sure you have some king of

<activity android:name=".Mainfragment" />
0
votes

Probably a declaration missing in your xml

Most likely

R.id.buttonNew

To clarify: your activity is looking for a button named buttonNew and not finding it

0
votes
here you have to mention your button in fragment_main.xml and you have to set 

activity_main.xml in setContentView() method then how it can possible to find your button just change the xml file name.

like below:

setContentView(R.layout.fragment_main);

0
votes
First of all , I read your all code ...Do you know, what u did big mistake..

In Activity

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

            newButton = (Button) findViewById(R.id.buttonNew);

Can you check out , where your define button id in (R.layout.activity_main)

Second Mistake in Fragment

getSupportFragmentManager().beginTransaction()
                        .add(R.id.container, new PlaceholderFragment())
                        .commit();
you have to call

getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
0
votes
package com.mvvmmodelrecyclerview;

import android.app.Activity;

import android.app.Fragment;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;

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.Button;
import android.os.Build;



public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);




        //FragmentManager manager = getSupportFragmentManager();
        if (savedInstanceState == null) {

            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();




        }


    }




    /**
     * A placeholder fragment containing a simple view.
     */

    public static class PlaceholderFragment extends Fragment {
        Button newButton;
        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            newButton = (Button) rootView.findViewById(R.id.buttonNew);
            newButton.setOnClickListener(new View.OnClickListener() {

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

                    Intent newNote = new Intent(getActivity(), NewNoteActivity.class);
                    startActivity(newNote);
                }
            });
            return rootView;
        }

        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {

            super.onActivityCreated(savedInstanceState);
        }
    }


}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/MainScreen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >


    <FrameLayout
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.src.vicnote.MainActivity"
        tools:ignore="MergeRootFrame" />
</LinearLayout>

fragment_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/MainScreen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <Button
        android:layout_gravity="center"
        android:id="@+id/buttonNew"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="New" />
    <TextView
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Fragmnt View" />

</LinearLayout>
0
votes
Or if you want to add button in activity_main.xml 

just as like your code did...then try to do it proper way...like that...


activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/MainScreen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <Button
        android:layout_gravity="center"
        android:id="@+id/buttonNew"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="New" />
    <FrameLayout
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.src.vicnote.MainActivity"
        tools:ignore="MergeRootFrame" />
</LinearLayout>


fragment_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/MainScreen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:gravity="center_vertical"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Fragmnt View" />

</LinearLayout>



MainActivity.java

package com.mvvmmodelrecyclerview;

import android.app.Activity;

import android.app.Fragment;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;

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.Button;
import android.os.Build;



public class MainActivity extends Activity {

    Button newButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        newButton = (Button) findViewById(R.id.buttonNew);
        newButton.setOnClickListener(new View.OnClickListener() {

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

                Intent newNote = new Intent(MainActivity.this, NewNoteActivity.class);
                startActivity(newNote);
            }
        });


        //FragmentManager manager = getSupportFragmentManager();
        if (savedInstanceState == null) {

            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();




        }


    }




    /**
     * 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 onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
        }
    }


}