Note that there are some explanatory texts on larger screens.

plurals
  1. POUnfortunately, UsingIntent has stopped
    primarykey
    data
    text
    <p>Whenever I click on second button, the error <code>Unfortunately,UsingIntent has stopped working</code> comes on app.</p> <p>It is a demo of use of Intents. </p> <p>activity_main.xml</p> <pre><code>&lt;LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity" &gt; &lt;TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /&gt; &lt;Button android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/btn_str1" android:onClick="onClick" /&gt; &lt;Button android:id="@+id/btn2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/btn2" android:onClick="onClick2" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>MainActivity.java</p> <pre><code>package com.example.usingintent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View v) { startActivityForResult(new Intent("com.example.usingintent.SecondActivity"),1); } public void onClick2(View v) { Intent i = new Intent("com.example.usingintent.ThirdActivity"); i.putExtra("name", "My Name is Khan"); i.putExtra("age", 32); Bundle extras = new Bundle(); extras.putString("name", "My name is Singh"); extras.putInt("age", 23); i.putExtras(extras); startActivityForResult(i,2); } public void onActivityResult(int requestcode,int resultcode,Intent i) { if(requestcode == 1) { if(resultcode == RESULT_OK) { Toast.makeText(this, i.getData().toString(), Toast.LENGTH_SHORT).show(); } } if(requestcode == 2) { if(resultcode==RESULT_OK) { Toast.makeText(this, i.getStringExtra("name"), Toast.LENGTH_SHORT).show(); Toast.makeText(this,i.getData().toString(), Toast.LENGTH_SHORT).show(); } } } @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; } } </code></pre> <p>AndroidManifest.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.usingintent" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="11" android:targetSdkVersion="18" /&gt; &lt;application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;activity android:name="com.example.usingintent.MainActivity" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;activity android:name=".SecondActivity" android:label="Second Activity"&gt; &lt;intent-filter &gt; &lt;action android:name="com.example.usingintent.SecondActivity"/&gt; &lt;category android:name="android.intent.category.DEFAULT" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;activity android:name=".ThirdActivity" android:label="Third Activity"&gt; &lt;intent-filter&gt; &lt;action android:name="com.example.usingintent.ThirdActivity"/&gt; &lt;category android:name="android.intent.category.DEFAULT" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>secondactivity.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;TextView android:text="@string/str_second" android:layout_width="match_parent" android:layout_height="wrap_content" /&gt; &lt;TextView android:text="@string/str_second1" android:layout_width="match_parent" android:layout_height="wrap_content" /&gt; &lt;EditText android:inputType="text" android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" /&gt; &lt;Button android:id="@+id/btn_second" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/btn_second1" android:onClick="onClick" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>thirdactivity.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;Button android:id="@+id/btn_third" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/str_btn2" android:onClick="onClick" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>SecondActivity.java</p> <pre><code>package com.example.usingintent; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class SecondActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.secondactivity); } public void onClick(View v) { Intent i = new Intent(); EditText edit = (EditText) findViewById(R.id.edit_text); i.setData(Uri.parse(edit.getText().toString())); setResult(RESULT_OK,i); finish(); } } </code></pre> <p>ThirdActivity.java</p> <pre><code> package com.example.usingintent; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class ThirdActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.thirdactivity); Toast.makeText(this, getIntent().getStringExtra("name"), Toast.LENGTH_SHORT).show(); Toast.makeText(this, getIntent().getIntExtra("age",100),Toast.LENGTH_SHORT).show(); Bundle b = getIntent().getExtras(); Toast.makeText(this, b.getString("name"),Toast.LENGTH_SHORT).show(); Toast.makeText(this, b.getInt("age"), Toast.LENGTH_SHORT).show(); } public void onClick(View v) { Intent i = new Intent(); i.putExtra("name", "Rahul"); i.setData(Uri.parse("Something passed to the main Activity")); setResult(RESULT_OK,i); finish(); } } </code></pre> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload