0

I have two Application that are supposed to work together. App1 has 2 text fields and run button which sends arguments from text fields into App2.

Problem is that pressing 'run' resets App1 with message: "projek5 keeps stopping"

Running App2 by itself returns toast "No arguments", so no problem here

Could it be a problem in App2 manifest file?

I am stuck with this problem since yesterday and tbh I would like to move to another project, I got 2 more to do...

Logcat: https://pastebin.com/7CbZF7c7

Link to files: https://filebin.net/tj7tvz2i6p90pfgz

App1 code:

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

App2 code:

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

App2 manifest:

<?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>
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Pakuss
  • 13
  • 5
  • 1
    Does this answer your question? [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – a_local_nobody Jan 22 '20 at 13:14

1 Answers1

0

You haven't specified the application package or class in your intent, you need to do this to send an intent to another application.

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

Update:

Receiving the intent and extracting the extras:

Intent intent = getIntent();
final int[] gatheredData = intent.getIntArrayExtra("EXTRA_GATHERED_DATA");

Or preferably, give each extra it's own key:

// 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");
JakeB
  • 2,043
  • 3
  • 12
  • 19
  • Thanks to this atleast I am getting toast from App2 informing me about arguments taken. But how do I make 'operation' and 'result' update? – Pakuss Jan 22 '20 at 14:06
  • Shouldn't this work? operationText.setText(result.getStringExtra(Intent.EXTRA_TEXT)); resultText.setText(String.valueOf(result.getIntExtra("value",0))); – Pakuss Jan 22 '20 at 14:12
  • Set what ever extras you plan to send in app1 and retrieve them with the appropriate key-values in app2. You don't need to use ```Intent.EXTRA_TEXT``` you can the key-value something more meaningful – JakeB Jan 22 '20 at 14:15
  • Updated answer to include some other formats for providing, receiving data. – JakeB Jan 22 '20 at 14:21
  • I think problem is in onActivityResult function, I debugged with toasts and function doesn't pass 'if conditions', so handleResult(data) doesn't update textView. These are the conditions: if(requestCode == UNIQUE_KEY && resultCode==RESULT_OK) – Pakuss Jan 22 '20 at 14:24
  • Find out which condition it fails on and progress from there.. – JakeB Jan 22 '20 at 14:27
  • After testing, handleResult appears to be a problem. Should I use then stuff from your updated post? For example should I replace my old code from Intent intent to sendIntent.setType("text/plain"); with your //Sending? – Pakuss Jan 22 '20 at 14:35