不幸的是,UsingIntent已停止

时间:2013-11-26 16:26:30

标签: java android xml android-intent

每当我点击第二个按钮时,错误Unfortunately,UsingIntent has stopped working就会出现在app上。

这是使用Intents的演示。

activity_main.xml中

<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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <Button 
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_str1"
        android:onClick="onClick"
        />
    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn2"
        android:onClick="onClick2"
        />

</LinearLayout>

MainActivity.java

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;
    }

}

的AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.usingintent.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>
        <activity 
            android:name=".SecondActivity"
            android:label="Second Activity">
            <intent-filter >
                <action android:name="com.example.usingintent.SecondActivity"/>
                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>
        <activity 
            android:name=".ThirdActivity"
            android:label="Third Activity">
            <intent-filter>
                <action android:name="com.example.usingintent.ThirdActivity"/>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

        </activity>
    </application>

</manifest>

secondactivity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<TextView 
    android:text="@string/str_second"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    />    
    <TextView
        android:text="@string/str_second1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        />

    <EditText
        android:inputType="text"
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <Button 
        android:id="@+id/btn_second"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_second1"
        android:onClick="onClick"
        />

</LinearLayout>

thirdactivity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<Button 
    android:id="@+id/btn_third"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/str_btn2"
    android:onClick="onClick"
    />    

</LinearLayout>

SecondActivity.java

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();
    }

}

ThirdActivity.java

  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();
    }

}

谢谢!

1 个答案:

答案 0 :(得分:0)

在你的清单中你有

<activity 
            android:name=".ThirdActivity"
            android:label="Third Activity">

因此

new Intent("com.example.usingintent.AThirdActivity")

需要

new Intent("com.example.usingintent.ThirdActivity")

并且java文件应保留为ThirdActivity.java。错误从那里出现的不是AThirdActivity.java文件和清单中的相应活动。