• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps help!! (Activity wont launch)

awaistoor

Lurker
hey im making a simple android application (basically its my first app after helloworld) .. i have an edittext and a button in main.xml and what i want is when that button click .. the text written in the edittext should b displayed in a dialog box .. these are the codes


code of main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">
    <EditText 
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
   
    android:text="@string/txt" 
    android:id="@+id/txtName"
    />
    
    <Button
    android:layout_height="wrap_content"
    android:id="@+id/btOk" 
    android:text="@string/bt" 
    android:layout_width="206dp" 
    android:layout_gravity="center"
    />
</LinearLayout>

code of strings.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, main!</string>
    <string name="app_name">ProjectTest</string>
    <string name="txt">Enter Your Name</string>
    <string name="bt">OK</string>
</resources>

code of main.java
Code:
package com.toor.projectTest;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class main extends Activity {
    /** Called when the activity is first created. */
	public Button btOK ;
	public EditText editName = new EditText(main.this);
	public String getName = new String();
	AlertDialog.Builder alertBox = new AlertDialog.Builder(main.this);

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    	editName = null;
    	getName = null;
        editName = (EditText) findViewById(R.id.txtName);
        btOK = (Button) findViewById(R.id.btOk);
        getName = editName.getText().toString();
        btOK.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				alertBox.setMessage(getName);
				alertBox.show();
			}
		});
        
    }
}


and whenever i run this app ... it crash ... and in logcat i get this error

07-09 06:59:15.296: ERROR/AndroidRuntime(763): FATAL EXCEPTION: main
07-09 06:59:15.296: ERROR/AndroidRuntime(763): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.toor.projectTest/com.toor.projectTest.main}: java.lang.NullPointerException
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at android.os.Looper.loop(Looper.java:123)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at java.lang.reflect.Method.invoke(Method.java:521)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at dalvik.system.NativeStart.main(Native Method)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): Caused by: java.lang.NullPointerException
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at android.view.View.<init>(View.java:1810)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at android.view.View.<init>(View.java:1856)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at android.widget.TextView.<init>(TextView.java:327)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at android.widget.EditText.<init>(EditText.java:55)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at android.widget.EditText.<init>(EditText.java:51)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at android.widget.EditText.<init>(EditText.java:47)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at com.toor.projectTest.main.<init>(main.java:14)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at java.lang.Class.newInstanceImpl(Native Method)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at java.lang.Class.newInstance(Class.java:1429)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
07-09 06:59:15.296: ERROR/AndroidRuntime(763): ... 11 more

so any1 can help me with that... i dont know where im going wrong ...

P.S. im new in this forum so will spend tym to understand it also :)
 
I can't help you programming-wise, but I think you need to edit your post title to be a bit more descriptive of your problem.
 
you can't reference "this" before onCreate();

So you need to change these lines:
Code:
public EditText editName = new EditText(main.this);
AlertDialog.Builder alertBox = new AlertDialog.Builder(main.this);
to

Code:
public EditText editName =null;
AlertDialog alertBox = null; 
// alertBox is not a builder... 
// but that's not why you're getting the error
then you can call new Whatever(this); later to build the objects

Also, be sure you have your manifest.xml setup properly but I'm pretty the above code is your problem, and welcome to AF!

Moving to App dev forum...
 
you can't reference "this" before onCreate();

So you need to change these lines:
Code:
public EditText editName = new EditText(main.this);
AlertDialog.Builder alertBox = new AlertDialog.Builder(main.this);
to

Code:
public EditText editName =null;
AlertDialog alertBox = null; 
// alertBox is not a builder... 
// but that's not why you're getting the error
then you can call new Whatever(this); later to build the objects

Also, be sure you have your manifest.xml setup properly but I'm pretty the above code is your problem, and welcome to AF!

Moving to App dev forum...

hey thanks .. this problem is solved ... but i have another query ... seeing the same code can you tell that y cant i gettext from edittext .. because whenever i push OK button .. the alertDialog box appears showing nothing ... same with the case if i use Toast for displaying text ... can you help me with that ??
 
Back
Top Bottom