-4

I have just started out with Android and Java, and i have just made a simple app with a couple of textviews, a button and a edittext.

When i run the app i get an error saying the: Unfortunately, FirstApp has stopped. I am familiar with programming, and wonder if i miss some library, or if ADT is misconfigured.

I have tried to clean and rebuild, to right click the project in the Package explorer and Android Tools -> Add support library, restarted the emulator, restarted ADT and restarted the computer. Cant figure out what does this.

Can any of you see something wrong here?

MainActivity.java imports

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

MainActivity.java

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


    Button btnCalc = (Button)findViewById(R.id.btnCalc);

    btnCalc.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            EditText number = (EditText)findViewById(R.id.num);
            TextView display = (TextView)findViewById(R.id.display);

            double num = Double.parseDouble(number.getText().toString());
            num = num * 5;
            display.setText(num + "");
        }
    });




    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.main, menu);
    return true;
}

fragment_main.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: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.danlevi.testproject.MainActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="@string/lblTitle" />

<EditText
    android:id="@+id/num"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp"
    android:ems="10"
    android:inputType="numberDecimal|numberSigned" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/btnCalc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/num"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="14dp"
    android:text="@string/btnCalc" />

<TextView
    android:id="@+id/display"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/btnCalc"
    android:layout_marginTop="16dp" />

</RelativeLayout>

Console

[2014-05-16 18:33:16 - TestProject] Dx 
trouble writing output: already prepared
[2014-05-16 18:33:17 - TestProject] ------------------------------
[2014-05-16 18:33:17 - TestProject] Android Launch!
[2014-05-16 18:33:17 - TestProject] adb is running normally.
[2014-05-16 18:33:17 - TestProject] Performing com.danlevi.testproject.MainActivity activity launch
[2014-05-16 18:33:17 - TestProject] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'Android'
[2014-05-16 18:33:17 - TestProject] Uploading TestProject.apk onto device 'emulator-5554'
[2014-05-16 18:33:18 - TestProject] Installing TestProject.apk...
[2014-05-16 18:33:30 - TestProject] Success!
[2014-05-16 18:33:30 - TestProject] Starting activity com.danlevi.testproject.MainActivity on device emulator-5554
[2014-05-16 18:33:33 - TestProject] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.danlevi.testproject/.MainActivity }

LogCat

05-16 12:33:37.412: D/AndroidRuntime(1579): Shutting down VM
05-16 12:33:37.412: W/dalvikvm(1579): threadid=1: thread exiting with uncaught exception (group=0xb2b08ba8)
05-16 12:33:37.432: E/AndroidRuntime(1579): FATAL EXCEPTION: main
05-16 12:33:37.432: E/AndroidRuntime(1579): Process: com.danlevi.testproject, PID: 1579
05-16 12:33:37.432: E/AndroidRuntime(1579): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.danlevi.testproject/com.danlevi.testproject.MainActivity}: java.lang.NullPointerException
05-16 12:33:37.432: E/AndroidRuntime(1579):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at android.os.Looper.loop(Looper.java:136)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at java.lang.reflect.Method.invokeNative(Native Method)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at java.lang.reflect.Method.invoke(Method.java:515)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at dalvik.system.NativeStart.main(Native Method)
05-16 12:33:37.432: E/AndroidRuntime(1579): Caused by: java.lang.NullPointerException
05-16 12:33:37.432: E/AndroidRuntime(1579):     at com.danlevi.testproject.MainActivity.onCreate(MainActivity.java:25)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at android.app.Activity.performCreate(Activity.java:5231)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-16 12:33:37.432: E/AndroidRuntime(1579):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-16 12:33:37.432: E/AndroidRuntime(1579):     ... 11 more
05-16 12:33:44.352: I/Process(1579): Sending signal. PID: 1579 SIG: 9
Dan-Levi Tømta
  • 796
  • 3
  • 14
  • 29
  • copy all the contents in fragment_main to activity_main.xml..This will solve the issue.. – Lal May 16 '14 at 16:48
  • This is the easiest and most simple solution..but the right way to do is specified in my answer..First try my comment,if that helps try my answer... – Lal May 16 '14 at 16:51
  • or replace `setContentView(R.layout.activity_main);` as `setContentView(R.layout.fragment_main);` – Lal May 16 '14 at 16:53
  • You should have been able to find the error yourself by using the debugger. – NickT May 16 '14 at 17:02
  • If that helped you kindly accept my answer....Please give me an upvote too..@Dan-LeviTomta – Lal May 16 '14 at 17:08
  • @NickT i have never touched Java before, and im not familiar with Eclipse. It was not that obvious for me but i see what you mean. – Dan-Levi Tømta May 16 '14 at 17:42

1 Answers1

1

This is because findViewById() searches in the activity_main layout, while the button is located in the fragment's layout fragment_main.

Move that piece of code in the onCreateView() method of the fragment:

//...

View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button buttonClick = (Button)rootView.findViewById(R.id.button);
buttonClick.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        onButtonClick((Button) view);
    }
});

Notice that now you access it through rootView view:

Button buttonClick = (Button)rootView.findViewById(R.id.button);

otherwise you would get again NullPointerException.

In your code, move

changer = (Button) findViewById(R.id.change1);
changeme = (TextView) findViewById (R.id.changeme1);
changer.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
changeme.setText("Changed");
}
});

to onCreateView() method of the fragment

Lal
  • 14,726
  • 4
  • 45
  • 70