-2

I'm working on an app where I have the option of "posting", when you press the post button you go to another activity where we have an EditText and a Button, once the Button is pressed the text should be taken and "posted" in the feed page in a ListView.

Now when I press the Button everything is working fine but nothing appears in the listview.

Here's the code:

Post:

package com.example.ali.test1;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;

public class Post extends ActionBarActivity {
        EditText input;

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

        // android:id="@+id/input"
        input = (EditText) findViewById(R.id.input);

    }


    public void addToList(View v) {
        Editable text = input.getText();
        Intent intent = new Intent();
        intent.putExtra("result", text);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }

    }

The class where the message should be seen:

package com.example.ali.test1;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;


public class MainActivity extends ActionBarActivity   {


    ListView list;
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list= (ListView) findViewById(R.id.list);

        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
        list.setAdapter(adapter);

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {


            if (requestCode == 1) {
                if (resultCode == Activity.RESULT_OK) {
                    String result = data.getStringExtra("result");
                    adapter.add(result);
                    adapter.notifyDataSetChanged();


                }
            }
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        int id2 = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        else if (id2 == R.id.action_cart) {
            startActivityForResult(new Intent(MainActivity.this , Post.class), 1);
        }
        return super.onOptionsItemSelected(item);
    }
}

The xml files:

Post:

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.ali.test1.Post">


    <EditText
        android:id="@+id/input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add to list"
        android:onClick="addToList"
        android:layout_below="@id/input"/>


</RelativeLayout>

Main:

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.ali.test1.MainActivity">


    <ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_weight="1" />


</RelativeLayout>

The problem is that nothing is showing in the listview any help?

  • 5
    Please post your logcat error trace. – Rohit5k2 Jan 21 '16 at 14:44
  • 1
    Without the error is a little hard to determine what is the problem. Please edit and post your LogCat error corresponding to the crash – diogojme Jan 21 '16 at 14:49
  • where can I find the logcat error trace in android studio? there's millions of lines in the logcat window – Ali Yassine Jan 21 '16 at 14:57
  • It would be in color **Red** when app crashes. – Rohit5k2 Jan 21 '16 at 14:58
  • It clearly says that you are trying to use the function "toString()" on an null object. So, maybe its just me, but it doesn't seem like the error is in the code you posted, because I can't see in the above code that you are using any toString method – DadoZolic Jan 21 '16 at 15:12
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – ʍѳђઽ૯ท Jan 21 '16 at 15:12
  • I will place my bet in this line: String result = data.getStringExtra("result"); If you see stack trace, the first "at" is in the arrayadapter. getStringExtra calls toString() internally, so probably your data object is null. You could set a breakpoint and debug to check it. – Fustigador Jan 21 '16 at 15:56

2 Answers2

0

Try checking the data for null value before adding and setting result.

if(data != null && data.getStringExtra("result") != null){
  String result = data.getStringExtra("result");
  adapter.add(result);
  adapter.notifyDataSetChanged();
}
DadoZolic
  • 177
  • 7
  • okay this worked and stopped the crashing but nothing showed in the listview any help? and thank you. – Ali Yassine Jan 21 '16 at 15:34
  • When you add directly to he adapter as you do by "adapter.add(result)" this automatically calls the notifyDataSetChanged function so try removing the notify function. if that doesn't work try using list.add(result) and then call list.getAdapter().notifyDataSetChanged(). You should aslo have a function getView() for your adapter, i recommend that you post that too in your question above – DadoZolic Jan 21 '16 at 15:51
  • He is using default adapter, which already has a getView() method. – Fustigador Jan 21 '16 at 16:01
  • Can't he override it ? – DadoZolic Jan 21 '16 at 16:04
0

Instead of String result=data.getStringExtra("result") try this way:

Bundle MBuddle = data.getExtras();
String MMessage = MBuddle.getString("result");
Fustigador
  • 6,339
  • 12
  • 59
  • 115