开发者

android intents

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(开发者_如何学GoUri.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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜