0

I am sorry in advance. I know this must be straight forward. Quite a novice with android. I am creating a main activity and trying to call another activity with the help of intent. Other activity contains a list view. I am trying to show simple strings in the list view. So I think that I don't need to write any of get view. Just AdapterView should work fine. Some code to explain more what I am upto is as follows : Main Activity button onClick method is as follows :

public void manage(View view) {
    Intent managerIntent;
           managerIntent = new Intent( this,ShowActivity.class);
    managerIntent.putExtra(FILE_URI, mediaStorageDir.getAbsolutePath());
    startActivity(managerIntent);
}

ShowActivity is as follows:

public class ShowActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayList<String> strings = new ArrayList<String>();
        Intent intent = getIntent();
        MyService sc = new MyService()
        sc.fill(strings,intent);

        final ArrayAdapter<String> ListObject = new ArrayAdapter<String>(ShowReceiptsActivity.this,
                android.R.layout.simple_list_item_1, strings);

        ListView listView = (ListView)findViewById(R.id.listView);// This line gives null pointer exception
        listView.setAdapter(ListObject);
        setContentView(R.layout.activity_show_receipts);
    }
}

activity_show.xml is as follows:

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.receiptboss.manager.ShowReceiptsActivity">

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.receiptboss.manager.ShowReceiptsActivity"/>
</LinearLayout>

Any help is greatly appreciated.

1
  • Did you try ma solution? Did it resolve your issue? Commented Feb 7, 2015 at 21:35

1 Answer 1

1

You have to call setContentView(R.layout.activity_show) in onCreate(Bundle saveInstanceState) method in ShowActivity class as in code below:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Here is your missing method
    // as param set R.layout.activity_show or R.layout.activity_show_receipts
    //it is depend of layout xml name 
    setContentView(R.layout.activity_show) 
    ....
}

You have to set your layout as content view in your activity, before you calling method findViewById(int) on your Activity. Now you're calling this method as last - it is wrong.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. Its solved. I knew it should some stupid thing.

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.