-1

I am in activity MainActivity which use the layout main2:

setContentView(R.layout.main2);

Now I want to get view "btnDisplay " from another layout called " row_in_list_test" .

I did :

LinearLayout row_in_list_tests = (LinearLayout) findViewById(R.id.row_in_list_tests);

btnDisplay = (Button) row_in_list_tests.findViewById(R.id.btnDisplay); //null pointer exception

btnDisplay.setOnClickListener(new OnClickListener() { ..

but in runTime i get "null pointer exception".

When I get btnDisplay in this way :

btnDisplay = (Button) findViewById(R.id.btnDisplay);

it is work good but then I get "null pointer exception" in the next line :

btnDisplay.setOnClickListener(new OnClickListener() { .. /null pointer exception

What is the way to do it? (I don't need to inflate " row_in_list_test" , it is just a layout for ArrayAdapter0

HpTerm
  • 8,151
  • 12
  • 51
  • 67
  • You're trying to get a view that does not exist. What do you want to achieve, really? – laalto Jun 13 '14 at 08:22
  • 1
    row_in_list_test is a linearlayout, not a View that's why you get the null pointer. Moreover you can only access elements of a view that has been inflated. – HpTerm Jun 13 '14 at 08:27
  • Are you making a custom list or what? Why do you need to access a button of other layout? – Zoran Jun 13 '14 at 08:29
  • I just want to define onClickListener on the button (the button is in xml file of layout I use as custom arrayAdapter) – user2837319 Jun 13 '14 at 10:23

2 Answers2

0

You only can get the views of a layout defined in: setContentView().

So there must be an other activity using this method to create a context:

 setContentView(R.layout.row_in_list_tests);

Now you can get access to this layout with the context of the other activity:

 btnDisplay = (Button) context.findViewById(R.id.btnDisplay);

How to get the context of other activities is an other problem, which is solved here.

Community
  • 1
  • 1
TheOnlyJakobob
  • 541
  • 3
  • 14
0

Try this:

bthDisplay.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {



            }
        });
kgandroid
  • 5,507
  • 5
  • 39
  • 69