srh snl
srh snl

Reputation: 807

The application has stopped working.Please try again - error in android

I'm new to android so I made this simple app that accepts a string , which is your name, and if the button is clicked, it will display "Hello World, I'm "

Now, the problem is that it wont work, it kept on giving me "The application has stopped working.Please try again" error.

Here's the code

 package com.example.helloworld;
 import android.os.Bundle; 
 import android.app.Activity; 
 import android.view.Menu; 
 import android.view.View; 
 import android.widget.Button;
 import android.widget.EditText; 
 import android.widget.TextView;

 public class MainActivity extends Activity {       
 Button button = > (Button) findViewById(R.id.BT);
 EditText textfield = (EditText) findViewById(R.id.ET);
TextView textview = (TextView) findViewById(R.id.TV);


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

         button.setOnClickListener(new View.OnClickListener() {
      @Override             
         public void onClick(View v) {
      // TODO Auto-generated method stub
      if (textfield.getText().toString().equalsIgnoreCase("Please type your name.."))
      {
            textfield.setText("");
      }
      else  if (textfield.getText().toString().equals(""))
      {
            textfield.setText("");
      }
      else
      {
            String name = textfield.getText().toString();
        textview.setText("Hello World!! I am " + name);
      }
    }
     });

     textfield.setOnFocusChangeListener(new View.OnFocusChangeListener() {
     @Override
     public void onFocusChange(View v, boolean hasFocus) 
     {
           // TODO Auto-generated method stub
        textfield.setText("");
     }
   });
   }

     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.activity_main, menu);
         return true;
     }
      }

Here's the layout

<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:orientation="vertical"
    tools:context=".MainActivity" >


    <EditText
        android:id="@+id/ET"
        android:text="@string/textfield_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"        
        android:inputType="textPersonName"
        android:layout_centerHorizontal="true"

        />

    <Button
        android:id="@+id/BT" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/b_concat"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/ET"
        />

    <TextView
        android:id="@+id/TV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="25sp"
        android:text="" 
        android:layout_below="@id/BT"
        />

</RelativeLayout>

Here's Android Manifest

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

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

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

</manifest>

And Here's the logcat

07-14 12:53:16.129: D/AndroidRuntime(615): Shutting down VM
07-14 12:53:16.249: W/dalvikvm(615): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-14 12:53:16.310: E/AndroidRuntime(615): FATAL EXCEPTION: main
07-14 12:53:16.310: E/AndroidRuntime(615): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.helloworld/com.example.helloworld.MainActivity}: java.lang.NullPointerException
07-14 12:53:16.310: E/AndroidRuntime(615):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
07-14 12:53:16.310: E/AndroidRuntime(615):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-14 12:53:16.310: E/AndroidRuntime(615):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-14 12:53:16.310: E/AndroidRuntime(615):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-14 12:53:16.310: E/AndroidRuntime(615):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-14 12:53:16.310: E/AndroidRuntime(615):  at android.os.Looper.loop(Looper.java:130)
07-14 12:53:16.310: E/AndroidRuntime(615):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-14 12:53:16.310: E/AndroidRuntime(615):  at java.lang.reflect.Method.invokeNative(Native Method)
07-14 12:53:16.310: E/AndroidRuntime(615):  at java.lang.reflect.Method.invoke(Method.java:507)
07-14 12:53:16.310: E/AndroidRuntime(615):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-14 12:53:16.310: E/AndroidRuntime(615):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-14 12:53:16.310: E/AndroidRuntime(615):  at dalvik.system.NativeStart.main(Native Method)
07-14 12:53:16.310: E/AndroidRuntime(615): Caused by: java.lang.NullPointerException
07-14 12:53:16.310: E/AndroidRuntime(615):  at android.app.Activity.findViewById(Activity.java:1647)
07-14 12:53:16.310: E/AndroidRuntime(615):  at com.example.helloworld.MainActivity.<init>(MainActivity.java:13)
07-14 12:53:16.310: E/AndroidRuntime(615):  at java.lang.Class.newInstanceImpl(Native Method)
07-14 12:53:16.310: E/AndroidRuntime(615):  at java.lang.Class.newInstance(Class.java:1409)
07-14 12:53:16.310: E/AndroidRuntime(615):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-14 12:53:16.310: E/AndroidRuntime(615):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
07-14 12:53:16.310: E/AndroidRuntime(615):  ... 11 more

Upvotes: 0

Views: 419

Answers (2)

Raghunandan
Raghunandan

Reputation: 133560

Move your initialization below setContentView

Button button;
EditText textField;
TextView textview;   
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main); // set the content to the activity
 button =  (Button) findViewById(R.id.BT); //initialize after setContentView
 textfield = (EditText) findViewById(R.id.ET);
 textview = (TextView) findViewById(R.id.TV);
  ... // rest of the code
 } 

You can findViewById of the current view hierarchy set to the activity. You have to initalize after setContentView. You have initialized outside onCreate. Initialization fails. Hence NullPointerException.

Upvotes: 0

Dixit Patel
Dixit Patel

Reputation: 3192

Simple you have to use this way

public class MainActivity extends Activity
 {
Button button ; 
EditText textfield; 
TextView textview ;

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

     button =  (Button) findViewById(R.id.BT);
     textfield = (EditText) findViewById(R.id.ET);
     textview = (TextView) findViewById(R.id.TV);
     .......
}

EDIT : you are initialize ui element before activity is created(View is not set) & must remember initialize ui element in onCreate(..) method after line of setContentView(Your layout filename);.

Upvotes: 3

Related Questions