0

I am working on android. I tried the following code but the application is not working and showing error dialog as application closed unexpectedly. It is showing error message as runtime error caused by java.lang.NullPointer exception.

I am including my code and manifest files here..

IntentsActivity.java

public class IntentsActivity extends Activity {
int request_code=1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            startActivityForResult(new Intent(IntentsActivity.this,AnotherActivity.class),request_code);
        }
    });
}
public void onActivityResult(int requestcode, int resultcode, Intent data){
    if(requestcode==request_code)
        if(resultcode==RESULT_OK)
            Toast.makeText(getBaseContext(), "Data returned is "+data.getData().toString(), Toast.LENGTH_SHORT).show(); 
}
}

AnotherActivity.java

public class AnotherActivity extends Activity{
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.anotherxml);



    Button btn=(Button)findViewById(R.id.button1);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            EditText edittext=(EditText)findViewById(R.id.editText1);

            Intent i=new Intent();
            i.setData(Uri.parse(edittext.getText().toString()));
            setResult(RESULT_OK,i);
            finish();
        }
    });
}
}

Manifestfile

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

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".IntentsActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
           <activity android:name=".AnotherActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.AnotherActivity" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    </manifest>

Here main.xml file consists a button and another.xml consists consists a EditText and a button. Can anyone give me the reason why the application is not working

user636207
  • 286
  • 2
  • 6
  • 14
  • When does the exception occur, as soon as the app starts up or when you launch the second activity i.e. after button click – D-Dᴙum Oct 09 '11 at 16:28
  • There is nothing obviously wrong with your code. Please post the stacktrace here. – Ash Oct 09 '11 at 16:51
  • When you debug your app, the debugger should tell you which line of which class generated the NullPointerException (in the top right corner of the debuging view in Eclipse). With this info, it might be easier to answer your question. =) – AntoineG Oct 09 '11 at 16:55
  • Exception occuring after button click – user636207 Oct 09 '11 at 17:21

1 Answers1

0

Cannot seem to find anything wrong with your code, my guess: EditText edittext=(EditText)findViewById(R.id.editText1); returns null?

Felipe
  • 196
  • 2
  • 7