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>