Sending data from a second activity to the first o

2019-07-15 04:37发布

问题:

I am currently working on an app that allows you to make a list of notes:

  • The list is viewable in the first activity
  • The new note button leads to a second activity with edit text and a few text formatting buttons ( Bold, Italic, bigger text, smaller)
  • The app allows you on long clicks to either edit or delete the notes in the list.

The problem is when I send data from one activity to another I get this error on the logs:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference

And that happens only when I try to send the data back to the first activity. When I try to save it internally from a button in the second activity the same method:

getText().toString() 

works just fine. Here is the code for sending/receiving data between activities and and the saving method.

public void sendMessage(View view)
{
    editText = (EditText) findViewById(R.id.add_text);
    String message = editText.getText().toString();
    Intent intent  = new Intent(this, MainActivity.class);
    intent.putExtra(key_Message, message);
    startActivity(intent);
}


@Override
protected void onResume() {
    super.onResume();
    Intent intent = getIntent();
    String message = intent.getStringExtra(key_Message);
    list.add(message);
    adapter.notifyDataSetChanged();
}

And here is the method for saving text internally ( which works fine). Note that I added the sendMessage function in the writeMessage(but commented it)

 public void writeMessage(View view)
{
    String Message = editText.getText().toString();
    String file_name = "Notes_file";
    try {
        FileOutputStream fileOutputStream = openFileOutput(file_name, MODE_PRIVATE);
        fileOutputStream.write(Message.getBytes());
        fileOutputStream.close();
        Toast.makeText(getApplicationContext(),"Message has been saved", Toast.LENGTH_SHORT).show();
        //String message = editText.getText().toString();
        //Intent intent  = new Intent(this, MainActivity.class);
        //intent.putExtra(key_Message, message);
        //startActivity(intent);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

Also another small problem I couldn't wrap my head around either:

public void shrinkText(View view)
{   float modifySize = editText.getTextSize();
    modifySize = modifySize - 3;
    editText.setTextSize(modifySize);
}

Is a small method that is supposed to shrinkText from an edit text when the user clicks a button. It always increases text. Some help would be appreciated.

public void writeMessage(View view)
{
    String Message = editText.getText().toString();
    String file_name = "Notes_file";
    try {
        FileOutputStream fileOutputStream = openFileOutput(file_name, MODE_PRIVATE);
        fileOutputStream.write(Message.getBytes());
        fileOutputStream.close();
        Toast.makeText(getApplicationContext(), "Message has been saved", Toast.LENGTH_SHORT).show();
        String message = editText.getText().toString();
        Intent intent  = new Intent();
        intent.putExtra(key_edit, message);
        setResult(RESULT_OK, intent);
        finish();


    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 1){
        if(resultCode == RESULT_OK){
            String item_tobeadded1;
            item_tobeadded1 = data.getStringExtra(key_edit);
            list.add(item_tobeadded1);
            //adapter.notifyDataSetChanged();
            adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1, list);
            list_view.setAdapter(adapter);
        }
    }
}

回答1:

As already suggested what you can do is start your second activity for with the data on the listview item you just clicked when you are editing, or when you crate a new don't pass anything.

In this manner you would be able to edit as well as create the new Note without making much changes in the code.

int RESPONSE_CODE = 123; // from this code will will differentiate if 
                         // the activity which we started actually did the task or not

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent intent = new Intent(getApplicationContext(), NoteEditActivity.class);
            intent.putString("para1","arg1");
            intent.putString("para2","arg2");
            intent.putString("para3","arg3");
            intent.putString("para4","arg4");
            startActivityForResult(intent, RESPONSE_CODE);
        }
    });

in NoteEditActivity.class you can receive your parameters if there were something. As while creating new Note para1,para2, para3 & para4 would be empty. But if the Note already exist you would have to edit that

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent myIntent = getIntent();
    String arg1 = myIntent.getStringExtra("para1");
    String arg2 = myIntent.getStringExtra("para2");
    String arg3 = myIntent.getStringExtra("para3");
    String arg4 = myIntent.getStringExtra("para4");
    // here you have to check that they or not null to differentiate 
    // if it is a new Note to be created OR you have to edit.
    // you should probably use some another way to figure this out, i am writing this because i would not be using it :)

}

And once you are done creating or editing your Note, attach the relevant data to the intent and finish the activity like this

Intent intent = new Intent();
intent.putExtra("para1","edited_data_1");
intent.putExtra("para2","edited_data_2");
intent.putExtra("para3","edited_data_3");
intent.putExtra("para4","edited_data_4");
setResult(RESULT_OK, intent);        
finish(); 

if the Task was successful you will set the result to RESULT_OK attach the intent and you are done

in the previous activity you have to override a method called onActivityResult() this method receives the data from the activity you had started to do some task,

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
         if(resultCode == RESULT_OK){

            String arg1 = data.getStringExtra("para1");
            String arg2 = data.getStringExtra("para2");
            String arg3 = data.getStringExtra("para3");
            String arg4 = data.getStringExtra("para4");
            // if you receive the data here,
            // you can add another element to List from which the -
            //`ListView` is populated and call `notifyDataSetChanged()` on the adapter.
            // like this `adapter.notifyDataSetChanged();` it will update the `listview` -
            //with new data that you received from the `NoteEditActivity.class` 

             adapter.notifyDataSetChanged(); // I am assuming you would add the changed item to the List
         }     
    }
}

Update, I have created a dummy code for you and it does works, i have tested it, I have also uploaded it on Github that you may check it there

NoteListActivity.java

public class NoteListActivity extends Activity {

    private ListView listView;
    private ArrayList<String> strings;
    public static final String NEW_ELEMENT = "NEW_ELEMENT";
    public static final String ELEMENT_VALUE = "ELEMENT_VALUE";
    public static final String ELEMENT_POSITION = "ELEMENT_POSITION";
    private ArrayAdapter<String> adapter;

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

        listView = (ListView) findViewById(R.id.NoteListActivity_listView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String text = (String) listView.getItemAtPosition(position);
                Intent intent = new Intent(getApplicationContext(), NoteEditActivity.class);
                intent.putExtra(NEW_ELEMENT, false);
                intent.putExtra(ELEMENT_POSITION, position);
                intent.putExtra(ELEMENT_VALUE, text);
                startActivityForResult(intent, 123);
            }
        });

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.NoteListActivity_fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), NoteEditActivity.class);
                intent.putExtra(NEW_ELEMENT, true);
                startActivityForResult(intent, 123);
            }
        });

        setUpList();

        adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.simple_list_item_1, strings);
        listView.setAdapter(adapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("NoteListActivity1",""+data.getStringExtra(NoteListActivity.ELEMENT_VALUE));

        if (requestCode == 123) {
            if (resultCode == RESULT_OK) {
                Log.d("NoteListActivity2",""+data.getStringExtra(NoteListActivity.ELEMENT_VALUE));
                boolean isNewElement = data.getBooleanExtra(NoteListActivity.NEW_ELEMENT, false);
                if (isNewElement) {
                    strings.add("" + data.getStringExtra(NoteListActivity.ELEMENT_VALUE));
                    adapter.notifyDataSetChanged();
                } else {
                    strings.set(data.getIntExtra(NoteListActivity.ELEMENT_POSITION, 0), "" + data.getStringExtra(NoteListActivity.ELEMENT_VALUE));
                    adapter.notifyDataSetChanged();
                }
            }
        }
    }

    private void setUpList() {
        strings = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            strings.add("Element_1_" + i);
        }
    }
}

activity_note_list.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    tools:context="com.nimgade.pk.mytutorialapplication.MainActivity">

    <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:orientation="vertical"
        tools:context="activities.list.first.NoteListActivity">


        <ListView
            android:id="@+id/NoteListActivity_listView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/NoteListActivity_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_menu_add" />

</android.support.design.widget.CoordinatorLayout>

NoteEditActivity.java

public class NoteEditActivity extends Activity {

    private EditText editText;
    private Button button;
    private boolean isNewElement;
    private int position;

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

        editText = (EditText) findViewById(R.id.NoteEditActivity_editText);
        button = (Button) findViewById(R.id.NoteEditActivity_button);

        Intent myIntent = getIntent();
        isNewElement = myIntent.getBooleanExtra(NoteListActivity.NEW_ELEMENT, false);

        if (!isNewElement) {
            editText.setText("" + myIntent.getStringExtra(NoteListActivity.ELEMENT_VALUE));
            position = myIntent.getIntExtra(NoteListActivity.ELEMENT_POSITION,0);
        }

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = editText.getText().toString();
                if (text != null && text.trim().length() > 0) {
                    Intent intent = new Intent();
                    Log.d("NoteEditActivity",""+text);
                    intent.putExtra(NoteListActivity.ELEMENT_VALUE, text);
                    intent.putExtra(NoteListActivity.NEW_ELEMENT, isNewElement);
                    if (!isNewElement) {
                        intent.putExtra(NoteListActivity.ELEMENT_POSITION, position);
                    }
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    Toast.makeText(getApplicationContext(), "Kindly write a valid element name", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

activity_note_edit.xml

<?xml version="1.0" encoding="utf-8"?>
<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:gravity="center"
    android:orientation="vertical"
    tools:context="activities.list.first.NoteEditActivity">


    <EditText
        android:id="@+id/NoteEditActivity_editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/NoteEditActivity_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save"
        android:textAllCaps="false" />
</LinearLayout>