anu
anu

Reputation: 719

NULL POINTER EXCEPTION for arrayadapter in listview. ANDROID

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();

Upvotes: 2

Views: 1136

Answers (2)

krishna
krishna

Reputation: 609

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);

Upvotes: 3

droidchef
droidchef

Reputation: 2307

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

Upvotes: 0

Related Questions