1

So I'm new to Android programming and I'm trying to make an app that takes input data and saves them to the phone then sends them out via email. I'm curently stuck. My app currently has inputs that don't save and a share button that can take a text string and put it as the text in a text message. That's about all I've figured out so far.

My question for right now is: How can I take the text from an input (editText1) and set it as plain text to the share feature?

I've tried a lot of different things I've seen online and while I can get them to become error free, when I actually open the app, it crashes/stops when I open the list item.

Here's my fragment code that works:

package com.example.nextslidemenuapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;


public class ItemDetailActivity extends FragmentActivity {

protected String mString;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_item_detail);

    // Show the Up button in the action bar.
    getActionBar().setDisplayHomeAsUpEnabled(true);

    // savedInstanceState is non-null when there is fragment state
    // saved from previous configurations of this activity
    // (e.g. when rotating the screen from portrait to landscape).
    // In this case, the fragment will automatically be re-added
    // to its container so we don't need to manually add it.
    // For more information, see the Fragments API guide at:
    //
    // http://developer.android.com/guide/components/fragments.html
    //
    if (savedInstanceState == null) {
        // Create the detail fragment and add it to the activity
        // using a fragment transaction.
        Bundle arguments = new Bundle();
        arguments.putString(ItemDetailFragment.ARG_ITEM_ID, getIntent()
                .getStringExtra(ItemDetailFragment.ARG_ITEM_ID));
        ItemDetailFragment fragment = new ItemDetailFragment();
        fragment.setArguments(arguments);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.item_detail_container, fragment).commit();
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int itemId = item.getItemId();

    if (itemId == R.id.action_share) {
        sharePost();
    }

    if (itemId == android.R.id.home) {
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpTo(this,
                new Intent(this, ItemListActivity.class));
        return true;
    }
    return super.onOptionsItemSelected(item);
}
final EditText ed1 = (EditText) findViewById(R.id.editText1);

String myString = "text";

private void sharePost() {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, myString);
    startActivity(Intent.createChooser(shareIntent,    
getString(R.string.share_chooser_title)));

}
}

And here is the specific code I'm focused on editing:

final EditText ed1 = (EditText) findViewById(R.id.editText1);

String myString = "text";

private void sharePost() {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, myString);
    startActivity(Intent.createChooser(shareIntent,
getString(R.string.share_chooser_title)));

}

I'm trying to replace "text" with the code that pulls editText1. Eventually I want to concatenate a number of the input boxes together with some semicolons.

Thanks for any help!

Gabriel Negut
  • 13,860
  • 4
  • 38
  • 45
  • I think you want to call the `ed1.getText()` method . More information at http://developer.android.com/reference/android/widget/EditText.html#getText() – Android2390 Sep 05 '13 at 20:43
  • I get an error that it can't convert editable to a String.. – user2752165 Sep 05 '13 at 20:52
  • try adding a .toString() in the end. Full line should be `ed1.getText().toString()` – Android2390 Sep 05 '13 at 20:55
  • I have: String myString = ed1.getText().toString(); It still didn't work. The app still stops. If I set it back to "text" it goes back to working. – user2752165 Sep 05 '13 at 20:57
  • `String myString = ed1.getText().toString();` is correct. This line pulls any text which is present in the editText and if it is null/empty then it will have an empty string. Can you post the LogCat messages ? If you are setting the `myString` to text then you are setting a fixed value. I thought you wanted to pull the text from the editText,right ? – Android2390 Sep 05 '13 at 21:01
  • You can't just define a variable "myString" with the value of a UI element that isn't created until runtime. You need to get a reference to ed1, pull the text out of it, and send it with your intent. So: shareIntent.putExtra(Intent.EXTRA_TEXT, ed1.getText().toString()); I would suggest reading some basic java tutorials before you dive as deeply into Android. – DiscDev Sep 05 '13 at 21:01
  • i dont know if this is right or not but your code snippet is inside the `onOptionsItemSelected` method . Try moving the editText declaration into the `onCreate` – Android2390 Sep 05 '13 at 21:06
  • So I tried the changed shareIntent and got the same result. I got rid of the string because, no, I don't want it saved permanently as a String, just sent. – user2752165 Sep 05 '13 at 21:08
  • I tried moving the code into onCreate and the ed1 couldn't be reached. – user2752165 Sep 05 '13 at 21:08
  • So here is the log cat before I click into the list fragment: 09-05 17:09:21.341: E/Trace(971): error opening trace file: No such file or directory (2) 09-05 17:09:22.131: D/gralloc_goldfish(971): Emulator without GPU emulation detected. 09-05 17:09:22.431: I/Choreographer(971): Skipped 36 frames! The application may be doing too much work on its main thread. – user2752165 Sep 05 '13 at 21:10
  • and after I click and get the stop message: 09-05 17:10:34.925: I/Choreographer(971): Skipped 35 frames! The application may be doing too much work on its main thread. 09-05 17:10:35.010: D/AndroidRuntime(971): Shutting down VM 09-05 17:10:35.010: W/dalvikvm(971): threadid=1: thread exiting with uncaught exception (group=0x40a13300) 09-05 17:10:35.050: E/AndroidRuntime(971): FATAL EXCEPTION: main 09-05 17:10:35.050: E/AndroidRuntime(971): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.nextslidemenuapp/com.example.nextslidemenuapp...plus much more – user2752165 Sep 05 '13 at 21:13

1 Answers1

0

Okay Trying to post the solution here :

EditText ed1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_detail);

ed1 = (EditText) findViewById(R.id.editText1);
}

private void sharePost() {
   Intent shareIntent = new Intent(Intent.ACTION_SEND);
   shareIntent.setType("text/plain");
   shareIntent.putExtra(Intent.EXTRA_TEXT, ed1.getText().toString());
   startActivity(Intent.createChooser(shareIntent,
   getString(R.string.share_chooser_title)));
}

This is not the end solution as your question is still very much vague. But this is how it is supposed to look like. Bottom line : instantiate an editText, pull data from editText and share that text via shareIntent.

Android2390
  • 1,150
  • 12
  • 21
  • This almost worked, I can get into the list item and fill out the input fields but when I go to click share it gives me the "app has stopped" error again. So close! – user2752165 Sep 06 '13 at 00:10