9

i have id "@+id/call" in single_item.xml when i use findVewById it (the layout setcontextview(R.layout.main)) .the app crash .how to fix the error

Community
  • 1
  • 1
bbkaaka
  • 297
  • 2
  • 5
  • 16
  • please show us the exact single_item.xml, the lines where you use setContextView(..) and findViewById(..) and please the logcat of the exception. Than we can provide you a lot quicker with the help you need. With the current information we have no idea where to look or how to help you. – Patrick Boos Nov 18 '10 at 03:10

5 Answers5

51

If you want to access a view in another layout (not the active layout), then you can inflate the layout you want to use and access it that way.

Example:

View inflatedView = getLayoutInflater().inflate(R.layout.other_layout, null);
TextView text = (TextView) inflatedView.findViewById(R.id.text_view);
text.setText("Hello!");

More information about inflating layouts can be found here.

Eddie
  • 1,968
  • 17
  • 19
  • i tried but it dont work correctly this is the the button in single_item.xml this is some code in class i want to get button public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); – bbkaaka Nov 19 '10 at 01:34
  • View inflatedView = getLayoutInflater().inflate(R.layout.single_item, null); Button call = (Button) inflatedView.findViewById(R.id.call); call.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //TODO Auto-generated method stub call(); } }); – bbkaaka Nov 19 '10 at 01:37
  • i want to use this get the id buton to make a call because the class CustomSqlCursorAdapter extends implements implecursoradapter i can't use this to do activity so i want to get this from managecontact – bbkaaka Nov 19 '10 at 02:02
  • when i click the button nothing happen :( – bbkaaka Nov 19 '10 at 02:06
  • So these are buttons inside list items? – Eddie Nov 19 '10 at 03:06
  • If you are dealing with items inside a ListView, check out http://stackoverflow.com/questions/2322390/android-row-becomes-unclickable-with-button/2323085#2323085. – Eddie Nov 19 '10 at 03:08
  • it 's also my prbolem .more inportantly, i want to make a call – bbkaaka Nov 19 '10 at 08:55
  • u can see the example http://androidforbeginners.blogspot.com/2010/03/clicking-buttons-in-listview-row.html and give me some solution – bbkaaka Nov 19 '10 at 08:57
0

You are trying to find a View (R.id.call) that is declared in R.layout.single_item in the layour R.layout.main, so I guess it is throwing a Null Pointer Exception.

You should either declare your "@+id/call" element in your main.xml file, or set the context view to R.layout.single_item

nbarraille
  • 9,926
  • 14
  • 65
  • 92
0

The simple way to fetch id from a view is:

String id = getResources().getResourceEntryName(view.getId());
Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52
0

If you want to get the id in the fragment use the following method. replace the return statement with view like this.

 View view= inflater.inflate(R.layout.fragment_left, container, false);
 img=view.findViewById(R.id.image);
 return view;

call all the methods before you return the view.

-2
View parent = (View)view.getParent();
??? = (???)parent.findViewById(R.id.call);

try this. hope it help

Farhad
  • 4,119
  • 8
  • 43
  • 66