Question

I really don't know whats wrong with this... The app should only change the text of the text view when clicking a button, in this case the start button.

When I start the app with the virtual machine it crashes.

My code

package com.example.projectiii;

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.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

@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();
    }


    Button btnS = (Button) findViewById(R.id.btStart);
    btnS.setOnClickListener(new OnClickListener()
    {

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

            TextView word = (TextView) findViewById(R.id.display);
            word.setText("Project");

        }

    });
}

@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;
    }
}

}

My 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.example.projectiii.MainActivity$PlaceholderFragment" >

<EditText
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/display"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="46dp"
    android:ems="10" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/btStart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/text"
    android:layout_below="@+id/text"
    android:layout_marginTop="42dp"
    android:text="Start" />

<Button
    android:id="@+id/btFinish"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/btStart"
    android:layout_alignRight="@+id/text"
    android:text="Finish" />

<Chronometer
    android:id="@+id/time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/text"
    android:layout_marginTop="40dp"
    android:text="Chronometer" />

<TextView
    android:id="@+id/display"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/time"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="66dp"
    android:text="Text"
    android:textSize="30sp" />

My logcat

05-03 15:06:48.720: E/AndroidRuntime(921): FATAL EXCEPTION: main
05-03 15:06:48.720: E/AndroidRuntime(921): Process: com.example.projectiii, PID: 921
05-03 15:06:48.720: E/AndroidRuntime(921): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projectiii/com.example.projectiii.MainActivity}: java.lang.NullPointerException
05-03 15:06:48.720: E/AndroidRuntime(921):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-03 15:06:48.720: E/AndroidRuntime(921):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-03 15:06:48.720: E/AndroidRuntime(921):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-03 15:06:48.720: E/AndroidRuntime(921):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-03 15:06:48.720: E/AndroidRuntime(921):  at android.os.Handler.dispatchMessage(Handler.java:102)
05-03 15:06:48.720: E/AndroidRuntime(921):  at android.os.Looper.loop(Looper.java:136)
05-03 15:06:48.720: E/AndroidRuntime(921):  at android.app.ActivityThread.main(ActivityThread.java:5017)
05-03 15:06:48.720: E/AndroidRuntime(921):  at java.lang.reflect.Method.invokeNative(Native Method)
05-03 15:06:48.720: E/AndroidRuntime(921):  at java.lang.reflect.Method.invoke(Method.java:515)
05-03 15:06:48.720: E/AndroidRuntime(921):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-03 15:06:48.720: E/AndroidRuntime(921):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-03 15:06:48.720: E/AndroidRuntime(921):  at dalvik.system.NativeStart.main(Native Method)
05-03 15:06:48.720: E/AndroidRuntime(921): Caused by: java.lang.NullPointerException
05-03 15:06:48.720: E/AndroidRuntime(921):  at com.example.projectiii.MainActivity.onCreate(MainActivity.java:33)
05-03 15:06:48.720: E/AndroidRuntime(921):  at android.app.Activity.performCreate(Activity.java:5231)
05-03 15:06:48.720: E/AndroidRuntime(921):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-03 15:06:48.720: E/AndroidRuntime(921):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-03 15:06:48.720: E/AndroidRuntime(921):  ... 11 more
05-03 15:06:48.760: W/ActivityManager(385):   Force finishing activity com.example.projectiii/.MainActivity
Was it helpful?

Solution

Make sure the xml you posted is activity_main, not fragment_main. You don't need a Fragment here, so remove all Fragment-related code.

OTHER TIPS

Bind the view of TextView here:

setContentView(R.layout.activity_main);
TextView word = (TextView) findViewById(R.id.display);

the layout xml you have shown is of the fragment(since it doesn't contain the R.id. container). You are trying to access the button element defined in your Fragment layout in your activity which is giving you he null pointer. Try accessing the button from the fragment class which will resolve your error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top