Android ';项目不断停止';尝试从其他应用程序获取数据后

Android ';项目不断停止';尝试从其他应用程序获取数据后,android,Android,我有两个应用程序应该一起工作。 有2个文本字段和运行按钮,用于将参数从文本字段发送到App2 问题是按“运行”重置App1,并显示消息:“projek5保持停止” 单独运行App2会返回toast“无参数”,因此这里没有问题 这可能是App2清单文件中的问题吗 从昨天开始我就一直在处理这个问题,我想换一个项目,我还有两个要做 日志: 链接到文件: App1代码: package com.example.student.projek5; import android.content.Intent

我有两个应用程序应该一起工作。 有2个文本字段和运行按钮,用于将参数从文本字段发送到App2

问题是按“运行”重置App1,并显示消息:“projek5保持停止”

单独运行App2会返回toast“无参数”,因此这里没有问题

这可能是App2清单文件中的问题吗

从昨天开始我就一直在处理这个问题,我想换一个项目,我还有两个要做

日志:

链接到文件:

App1代码:

package com.example.student.projek5;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    final int UNIQUE_KEY=777;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(requestCode == UNIQUE_KEY && resultCode==RESULT_OK)
        {
            handleResult(data);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void handleResult(Intent result)
    {
        TextView resultText = (TextView) findViewById(R.id.ResultText);
        TextView operationText = (TextView) findViewById(R.id.OperationText);

        operationText.setText(result.getStringExtra(Intent.EXTRA_TEXT));
        resultText.setText(String.valueOf(result.getIntExtra("value",0)));
    }

    public void run(View view)
    {
        Intent sendIntent= new Intent();
        sendIntent.setAction("projek5add");
        sendIntent.putExtra(Intent.EXTRA_TEXT, gatherData());
        sendIntent.setType("text/plain");
        startActivityForResult(sendIntent, UNIQUE_KEY);
    }

    private int[] gatherData()
    {
        TextView arg1=(TextView) findViewById(R.id.arg1);
        TextView arg2=(TextView) findViewById(R.id.arg2);
        return new int[]{Integer.parseInt(nullCheck(arg1.getText().toString())),
        Integer.parseInt(nullCheck(arg2.getText().toString()))};

    }

    private String nullCheck(String txt)
    {
        return txt.equals("") ? "0" : txt;
    }
}
package com.example.student.projek5add;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    final String operation="addition";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        showToast("Starting"+operation+"subactivity");
        Intent intent=getIntent();
        int[] data=intent.getIntArrayExtra(Intent.EXTRA_TEXT);
        if(data==null)
        {
            showToast("no arguments");
            finish();
            return;
        }
        showToast("arguments: "+data[0]+", "+data[1]);
        Intent result =new Intent();
        result.putExtra(Intent.EXTRA_TEXT, operation);
        result.putExtra("value", handle(data));
        setResult(Activity.RESULT_OK, result);
        finish();
    }

    private int handle(int[] data)
    {
        return data[0]+data[1];
    }
    private void showToast(String message)
    {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.student.projek5add">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.example.projek5.run"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="text/plain"/>
            </intent-filter>
        </activity>
    </application>

</manifest>
App2代码:

package com.example.student.projek5;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    final int UNIQUE_KEY=777;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(requestCode == UNIQUE_KEY && resultCode==RESULT_OK)
        {
            handleResult(data);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void handleResult(Intent result)
    {
        TextView resultText = (TextView) findViewById(R.id.ResultText);
        TextView operationText = (TextView) findViewById(R.id.OperationText);

        operationText.setText(result.getStringExtra(Intent.EXTRA_TEXT));
        resultText.setText(String.valueOf(result.getIntExtra("value",0)));
    }

    public void run(View view)
    {
        Intent sendIntent= new Intent();
        sendIntent.setAction("projek5add");
        sendIntent.putExtra(Intent.EXTRA_TEXT, gatherData());
        sendIntent.setType("text/plain");
        startActivityForResult(sendIntent, UNIQUE_KEY);
    }

    private int[] gatherData()
    {
        TextView arg1=(TextView) findViewById(R.id.arg1);
        TextView arg2=(TextView) findViewById(R.id.arg2);
        return new int[]{Integer.parseInt(nullCheck(arg1.getText().toString())),
        Integer.parseInt(nullCheck(arg2.getText().toString()))};

    }

    private String nullCheck(String txt)
    {
        return txt.equals("") ? "0" : txt;
    }
}
package com.example.student.projek5add;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    final String operation="addition";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        showToast("Starting"+operation+"subactivity");
        Intent intent=getIntent();
        int[] data=intent.getIntArrayExtra(Intent.EXTRA_TEXT);
        if(data==null)
        {
            showToast("no arguments");
            finish();
            return;
        }
        showToast("arguments: "+data[0]+", "+data[1]);
        Intent result =new Intent();
        result.putExtra(Intent.EXTRA_TEXT, operation);
        result.putExtra("value", handle(data));
        setResult(Activity.RESULT_OK, result);
        finish();
    }

    private int handle(int[] data)
    {
        return data[0]+data[1];
    }
    private void showToast(String message)
    {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.student.projek5add">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.example.projek5.run"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="text/plain"/>
            </intent-filter>
        </activity>
    </application>

</manifest>
App2清单:

package com.example.student.projek5;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    final int UNIQUE_KEY=777;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(requestCode == UNIQUE_KEY && resultCode==RESULT_OK)
        {
            handleResult(data);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void handleResult(Intent result)
    {
        TextView resultText = (TextView) findViewById(R.id.ResultText);
        TextView operationText = (TextView) findViewById(R.id.OperationText);

        operationText.setText(result.getStringExtra(Intent.EXTRA_TEXT));
        resultText.setText(String.valueOf(result.getIntExtra("value",0)));
    }

    public void run(View view)
    {
        Intent sendIntent= new Intent();
        sendIntent.setAction("projek5add");
        sendIntent.putExtra(Intent.EXTRA_TEXT, gatherData());
        sendIntent.setType("text/plain");
        startActivityForResult(sendIntent, UNIQUE_KEY);
    }

    private int[] gatherData()
    {
        TextView arg1=(TextView) findViewById(R.id.arg1);
        TextView arg2=(TextView) findViewById(R.id.arg2);
        return new int[]{Integer.parseInt(nullCheck(arg1.getText().toString())),
        Integer.parseInt(nullCheck(arg2.getText().toString()))};

    }

    private String nullCheck(String txt)
    {
        return txt.equals("") ? "0" : txt;
    }
}
package com.example.student.projek5add;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    final String operation="addition";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        showToast("Starting"+operation+"subactivity");
        Intent intent=getIntent();
        int[] data=intent.getIntArrayExtra(Intent.EXTRA_TEXT);
        if(data==null)
        {
            showToast("no arguments");
            finish();
            return;
        }
        showToast("arguments: "+data[0]+", "+data[1]);
        Intent result =new Intent();
        result.putExtra(Intent.EXTRA_TEXT, operation);
        result.putExtra("value", handle(data));
        setResult(Activity.RESULT_OK, result);
        finish();
    }

    private int handle(int[] data)
    {
        return data[0]+data[1];
    }
    private void showToast(String message)
    {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.student.projek5add">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.example.projek5.run"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="text/plain"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

如果尚未在意图中指定应用程序包或类,则需要执行此操作才能将意图发送到其他应用程序

private static final String PACKAGE = "com.example.student.projek5add";
private static final String CLASS = "com.example.student.projek5add.MainActivity"

...

Intent sendIntent = getApplicationContext().getPackageManager().getLaunchIntentForPackage(PACKAGE);
sendIntent.setAction(Intent.ACTION_MAIN);
sendIntent.setComponent(new ComponentName(PACKAGE, CLASS));
sendIntent.putExtra("EXTRA_GATHERED_DATA", gatherData());
sendIntent.setType("text/plain");
startActivityForResult(sendIntent, UNIQUE_KEY);
更新:

package com.example.student.projek5;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    final int UNIQUE_KEY=777;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(requestCode == UNIQUE_KEY && resultCode==RESULT_OK)
        {
            handleResult(data);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void handleResult(Intent result)
    {
        TextView resultText = (TextView) findViewById(R.id.ResultText);
        TextView operationText = (TextView) findViewById(R.id.OperationText);

        operationText.setText(result.getStringExtra(Intent.EXTRA_TEXT));
        resultText.setText(String.valueOf(result.getIntExtra("value",0)));
    }

    public void run(View view)
    {
        Intent sendIntent= new Intent();
        sendIntent.setAction("projek5add");
        sendIntent.putExtra(Intent.EXTRA_TEXT, gatherData());
        sendIntent.setType("text/plain");
        startActivityForResult(sendIntent, UNIQUE_KEY);
    }

    private int[] gatherData()
    {
        TextView arg1=(TextView) findViewById(R.id.arg1);
        TextView arg2=(TextView) findViewById(R.id.arg2);
        return new int[]{Integer.parseInt(nullCheck(arg1.getText().toString())),
        Integer.parseInt(nullCheck(arg2.getText().toString()))};

    }

    private String nullCheck(String txt)
    {
        return txt.equals("") ? "0" : txt;
    }
}
package com.example.student.projek5add;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    final String operation="addition";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        showToast("Starting"+operation+"subactivity");
        Intent intent=getIntent();
        int[] data=intent.getIntArrayExtra(Intent.EXTRA_TEXT);
        if(data==null)
        {
            showToast("no arguments");
            finish();
            return;
        }
        showToast("arguments: "+data[0]+", "+data[1]);
        Intent result =new Intent();
        result.putExtra(Intent.EXTRA_TEXT, operation);
        result.putExtra("value", handle(data));
        setResult(Activity.RESULT_OK, result);
        finish();
    }

    private int handle(int[] data)
    {
        return data[0]+data[1];
    }
    private void showToast(String message)
    {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.student.projek5add">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.example.projek5.run"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="text/plain"/>
            </intent-filter>
        </activity>
    </application>

</manifest>
接收意图并提取额外信息:

Intent intent = getIntent();
final int[] gatheredData = intent.getIntArrayExtra("EXTRA_GATHERED_DATA");
或者,最好为每个额外的用户提供自己的密钥:

// Sending

Intent intent = new Intent();
intent.putExtra("EXTRA_ONE", valueOne);
intent.putExtra("EXTRA_TWO", valueTwo);

// Receiving
Intent intent = getIntent();
final int valueOne = intent.getIntExtra("EXTRA_ONE");
final int valueOne = intent.getIntExtra("EXTRA_TWO");

这回答了你的问题吗?至少多亏了这一点,我得到了App2的祝酒词,告知我所进行的辩论。但是我如何更新“操作”和“结果”呢?这不应该吗?operationText.setText(result.getStringExtra(Intent.EXTRA_TEXT));resultText.setText(String.valueOf(result.getIntExtra(“value”,0));设置计划在app1中发送的附加项,并在app2中使用适当的键值检索它们。你不需要使用
Intent。额外的文本
你可以在键值中添加一些更有意义的更新答案,以包含一些用于提供、接收数据的其他格式。我认为问题出在onActivityResult函数中,我用toasts进行了调试,函数没有传递“if conditions”,所以HandlerResult(数据)不会更新textView。以下是条件:if(requestCode==UNIQUE\u KEY&&resultCode==RESULT\u OK)