2

I have a main view(listview) and a second main view(edittext). I pass data from second main view backwards to first main view using startActivityforResult(). I have tested it out and I recieve the string back with no problem. However there are some problems displaying it on listview. My listview has two xml files one for the listview layout and one for the textview row. I have no idea why I am getting a nullpointer exception.

03-14 22:50:33.429    9268-9268/com.example.anusha.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.anusha.app, PID: 9268
java.lang.NullPointerException
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:392)
        at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
        at android.widget.AbsListView.obtainView(AbsListView.java:2780)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1274)
        at android.widget.ListView.onMeasure(ListView.java:1186)
        at android.view.View.measure(View.java:17594)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:17594)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:17594)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)
        at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
        at android.view.View.measure(View.java:17594)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:17594)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:17594)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2588)
        at android.view.View.measure(View.java:17594)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2308)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2030)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1267)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6640)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:813)
        at android.view.Choreographer.doCallbacks(Choreographer.java:613)
        at android.view.Choreographer.doFrame(Choreographer.java:583)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5635)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
        at dalvik.system.NativeStart.main(Native Method)

Here is my first main view with my listview. I'm pretty sure the problem is with the arrayadapter. Don't know what it is though.

 ArrayList<String> array= new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_address_list);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.activity_address_list,R.id.rowTextView, array);
    ListView listview = (ListView) findViewById(R.id.listView);
    listview.setAdapter(adapter);



}
private void Add() {
    Intent intent = new Intent(AddressList.this, SilentGeofence.class);
    startActivityForResult(intent, 2);

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 2) {
        if (resultCode == RESULT_OK){
            String result = data.getStringExtra("result");
            array.add(result);


        }
    }
    }

Here is my second main

   Button add = (Button) findViewById(R.id.button);
    add.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            String ag = txt1.getText().toString().trim();
                txt1.setText("");
                Intent returnIntent = new Intent();
                returnIntent.putExtra("result",ag);
                setResult(RESULT_OK,returnIntent);
                finish();
8
  • array is not populated with items. Where do you call Add()?? Commented Mar 15, 2015 at 6:10
  • your problem is ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.activity_address_list,R.id.rowTextView, array);. read documentation to how to populate arrayAdapter Commented Mar 15, 2015 at 6:11
  • i call Add() onOptionsItemSelected() in menu.It's one of my bar button items. @Raghunandan Commented Mar 15, 2015 at 6:12
  • I am using public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects) @shayanpourvatan Commented Mar 15, 2015 at 6:16
  • The array is getting items from the second main and getting updated in onActivityResult() right? Commented Mar 15, 2015 at 6:17

2 Answers 2

3

public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)

Added in API level 1 Constructor

Parameters context The current context. resource The resource ID for a layout file containing a layout to use when instantiating views. textViewResourceId The id of the TextView within the layout resource to be populated objects The objects to represent in the ListView.

So you should use layout of the textview not the listview layout see here see here

    new ArrayAdapter<>(this, R.layout.layout_name_of_the_xml_containing_textview,R.id.rowTextView, array);
Sign up to request clarification or add additional context in comments.

Comments

0

You must initialise your list in onCreate(), Just after the getSupportActionBar().setDisplayHomeAsUpEnabled(true);

And your ArrayAdapter<String> adapter should be a field not a local variable because, after you do array.add(result) you must also call adapter.notifyDataSetChanged() in order to refresh and populate the results in the listview

2 Comments

didn't work. I made a Array Adapter a field variable and put adapter.notifyDataSetChanged() after array.add(result) and the app still crashed @Ishan Khanna
it crashes saying: System services not available to Activities before onCreate() @ishan-khanna

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.