-1

I know the above question have been ask several times. The previous answered thread does not provide me with the solution.

What i am trying to do is to click on my listview because i set the style with a dialog theme.

When i launch the activity it will appear like a dialog. In the dialog i applied listview.

I populate the listview with my sql lite data.

What i am trying to do now is that. I want to retrieve arraylist data. I successfully got the listview data when chosen.

But while setting text to the listview on my main activity. Even having :

EditText Messages = (EditText) findviewbyid(R.id.Messages);
Messages.setText(arr.get(i).tostring); <--------

This is where i got error.

Below is the error message.

07-24 10:08:41.803    6721-6721/com.example.xaviertang.phonewithsms E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.xaviertang.phonewithsms, PID: 6721
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.append(java.lang.CharSequence)' on a null object reference
            at com.example.xaviertang.phonewithsms.listData$1.onItemClick(listData.java:38)
            at android.widget.AdapterView.performItemClick(AdapterView.java:299)
            at android.widget.AbsListView.performItemClick(AbsListView.java:1156)
            at android.widget.AbsListView$PerformClick.run(AbsListView.java:2963)
            at android.widget.AbsListView$3.run(AbsListView.java:3856)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5118)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
Codebender
  • 13,431
  • 6
  • 40
  • 83
hamham
  • 21
  • 6

2 Answers2

0

Typically this is an issue when you are trying to access a view before you have called setContentView();. Make sure that is called before you try to use findViewById().

Of course it is hard to know for sure without all the code.

mattfred
  • 2,583
  • 1
  • 20
  • 36
  • What if i am trying to post a text in another activity. Which set a view for the dialog but not the main. That means i am trying to post a text from the dialog activity that shows all my listview data after clicking it will set a text to my main activity with the text click. I'm sure the content view i set before the findviewbyid. Just that the content view set is for dialog not main. But Messages is set in main but not in dialog. – hamham Jul 24 '15 at 02:17
  • If you are trying to send the text to a different activity, you probably will need to send it through an Intent. You won't be able to access the view elements from another activity directly. – mattfred Jul 24 '15 at 02:20
0

I solve my own question. =D YAY!~~~~ And thanks to mattfred for telling me to use intent.

intent guide link : Intent Guide

Here goes.

On my listData Activity in my onclicklistener for list view.

  Intent i = new Intent(listData.this, MainActivity.class);
            String strName = array_list.get(position).toString();
            i.putExtra("STRING_I_NEED", strName);
            startActivity(i);

On my MainActivity in the onCreate method.

 String newString;
    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if(extras == null) {
            newString= null;
        } else {
            newString= extras.getString("STRING_I_NEED");
        }
    } else {
        newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
    }

Below setContentView of Main Activity this code is being inserted

 try{
        Messages = (EditText)findViewById(R.id.Messages);
        Messages.setText(newString);
    }catch(NullPointerException e){}

It has its flaw but it works my message saver app. =D WEEEEEEEE xD

Below are the screenshots.

enter image description hereenter image description here

hamham
  • 21
  • 6