EditText is null - android

I can't figure out why my EditText is null.
public ChoiceView(Context context) {
super(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflater.inflate(R.layout.choice_view, this, true);
Log.d("TEST CHOICEVIEW", "1");
et = (EditText) view.findViewById(R.id.enterChoice);
if (et == null) Log.d("TEST CHOICEVIEW", "null"); // << this always produces "null" in Logcat
tv = (TextView) findViewById(R.id.choiceViewTextView);
Log.d("TEST CHOICEVIEW", "2");
choiceString = et.getText().toString();
Log.d("TEST CHOICEVIEW", "3");
tv.setText(choiceString);
Log.d("TEST CHOICEVIEW", "4");
requestLayout();
}
and the xml file where the EditText is found (is not choice_view...is that an issue?):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/enterChoice"
android:hint="#string/enterChoiceText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</ScrollView>
<Button
android:id="#+id/saveButton"
android:text="#string/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
and just for the sake of thoroughness, the choice_view.xml file associated with the ChoiceView class:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/choiceViewTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</FrameLayout>
When I run this, Logcat produces the following before crashing:
TEST CHOICEVIEW 1
TEST CHOICEVIEW null
TEST CHOICEVIEW 2
AndroidRuntime Shutting down VM
Let me know if you need any additional information. Thanks!
Additional info:
ChoiceView is called from the ChoiceList class from the following code:
Context context = ChoiceList.this;
ChoiceView cv = new ChoiceView(context);

You need the component you are trying to reference to be contained in the XML layout you are inflating.
It looks to me like you are trying to reference Views (EditText) from more than 1 XML file. This is not appropriate
This why you are not finding the view you are looking for - choiceViewTextView
It is in NOT in the choice_view.xml file (which is the one you are inflating). Changing this call to reference the layout.xml file (you didn't specify the name for this file) will fix your issue.

Have you tried inflating the view (assuming the code you posted is in code_view) without attaching it to a view group? Like so:
final View view = inflater.inflate(R.layout.choice_view, null);
Caused me major headaches in the past.

Related

Android FindViewById is only working for a child layout (listview row) and not for TextViews in the parent layout

I am guessing my problem is with inflating layouts. I have 2 layout files:
activity_mail.xlm:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/results"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/profilesCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="18sp" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
... and listitem_device.xlm:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/device_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"/>
<TextView
android:id="#+id/device_service"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"/>
</LinearLayout>
in my main_activity java file, I have:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
....
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if (view == null) {
view = mInflator.inflate(R.layout.listitem_device, null);
viewHolder = new ViewHolder();
viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_label); // from listitem_device.xml
viewHolder.profilesCount = (TextView) view.findViewById(R.id.profilesCount); // from activity_main.xml
The problem is the subsequent code:
viewHolder.profilesCount.setText("HELLO");
generates the error "Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference".
How do I fix this?
I don't see anywhere that you have even have a view with id "profilesCount".You only have device label and device service as ids in the layout file that you are using to inflate the rows of the list view.Better use Recycler view also, it provides more flexibility.
Also, you can't access something from activity_mail.xml in the getView() function of your adapter because by using "view" in getfunction, as that corrspond to only the row of the list view and since that id is not present in the xml file associated with the row of list view, you are getting "viewholders.profileCount" as null and hence the Nullpointer exception.
If you wanna use a TextView or any view in your adapter, better create an interface and implement that interface in your activity to use as a call back and then set the textview field of your activity.xml file, thats the best way as it would keep your code cleaner.Other way is to pass the reference of the activity to your adapter and then find the text view or whatever that you wanna use from actvity_mail.xml file, but its not recommended and is a very bad way as it may require additional memory.

Sony SmartEyeglass overlapping layout

I am trying to implement card text change on Sony SmartEyeglass, but I am having trouble with the layouts. I adopted the advanced layout sample from the SDK and modified it.
I have a default xml layout that displays a title and a body text. I put 'Title' and 'Body' as default strings in the xml file, and tried to update the title and body as 'Updated' and 'Updated body text'. However, the result shows me the default layout (with 'Title' and 'Body') and the text 'Updated body text' overlapping on top of them.
Why is the title not edited, and how come the body text is on top of the xml TextView?
Here is the relevant code:
#Override
public void onResume() {
showingDetail = false;
ControlListItem item = createControlListItem(namePosition);
showLayout(item.dataXmlLayout, null);
sendListCount(item.layoutReference, quotedPeople.size());
sendListPosition(item.layoutReference, namePosition);
//utils.sendTextViewLayoutId(R.id.names_body);
sendListItem(item);
}
private ControlListItem createControlListItem(final int position) {
Log.d(Constants.LOG_TAG, "position = " + position);
ControlListItem item = new ControlListItem();
item.layoutReference = R.id.names_gallery;
item.dataXmlLayout = R.layout.smarteyeglass_quotes_names_gallery;
item.listItemId = position;
item.listItemPosition = position;
List<Bundle> list = new ArrayList<Bundle>();
// Header data
Bundle headerBundle = new Bundle();
headerBundle.putInt(Control.Intents.EXTRA_LAYOUT_REFERENCE, R.id.names_title);
headerBundle.putString(Control.Intents.EXTRA_TEXT, "Updated");
list.add(headerBundle);
// Body data
Bundle bodyBundle = new Bundle();
bodyBundle.putInt(Control.Intents.EXTRA_LAYOUT_REFERENCE, R.id.names_body);
bodyBundle.putString(Control.Intents.EXTRA_TEXT, "Updated body title");
list.add(bodyBundle);
item.layoutData = list.toArray(new Bundle[list.size()]);
return item;
}
Here is the xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="#dimen/smarteyeglass_control_width"
android:layout_height="#dimen/smarteyeglass_control_height"
tools:ignore="PxUsage,UselessParent,HardcodedText" >
<TextView
android:id="#+id/names_title"
android:layout_width="400px"
android:layout_height="30px"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginTop="5px"
android:paddingLeft="6px"
android:background="#android:color/black"
android:text="Title"
android:textColor="#android:color/white"
android:textSize="#dimen/smarteyeglass_text_size_normal" />
<View
android:id="#+id/names_divider"
android:layout_width="400px"
android:layout_height="2px"
android:layout_alignParentLeft="true"
android:layout_below="#+id/names_title"
android:background="#android:color/white" />
<TextView
android:id="#+id/names_body"
android:layout_width="400px"
android:layout_height="78px"
android:layout_marginTop="5px"
android:layout_marginLeft="10px"
android:layout_alignParentLeft="true"
android:layout_below="#+id/names_divider"
android:textColor="#android:color/white"
android:textSize="#dimen/smarteyeglass_text_size_small"
android:text="Body" />
<Gallery
android:id="#+id/names_gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</Gallery>
</RelativeLayout>
we just tested your example and the text is changing fine. It looks like you might have picked the wrong XML for the gallery item. You started with smarteyeglass_layout_test_gallery instead using smarteyeglass_item_gallery.

NullPointerException on getTextView in Android

so I have this getter function in a Class called ItemEnergyGreen:
public TextView getTextView(FrameLayout fl) {
textView = (TextView) fl.findViewById(R.id.textview_energyOrange_quantity);
return textView;
}
And I want now to call this methode from another Class:
//Item Energy Green
itemEnergyGreenFrameLayout = (FrameLayout) inflater.inflate(R.layout.item_energy_green_layout, null, false);
itemEnergyGreenQuantityTextView = itemEnergyGreen.getTextView(itemEnergyGreenFrameLayout);
btn_ItemEnergyGreen = (ImageButton) itemEnergyGreenFrameLayout.findViewById(R.id.btn_energyGreenItem_use);
btn_ItemEnergyGreen.setOnClickListener(this);
The function call is in Line 2, I am passing the frameLayout to the getter Function but textView in my ItemEnergyGreen class is always NULL.
The error occurs when i want to call setText on my view:
public void itemEnergyGreenUpdateNumber() {
int quantity = itemEnergyGreen.getQuantity();
String qstring = String.valueOf(quantity);
itemEnergyGreenQuantityTextView.setText(qstring);
}
This is the XML-FrameLayout that is get inflated:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ItemEnergyGreenFrameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageButton
android:id="#+id/btn_energyGreenItem_use"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/item_energydrink_green" >
</ImageButton>
<TextView
android:id="#+id/textview_energyGreen_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#F00"
android:clickable="false"
android:padding="2dp"
android:textColor="#FFF"
android:textStyle="bold" >
</TextView>
</FrameLayout>
Why is that?
ps. At the point where the getter function is called, the itemEnergyGreen Object is already initialized.
Thank you for your answers, and sorry if that is a dump question but i´m going crazy about this.
textView in my ItemEnergyGreen class is always NULL.
findViewById looks for a view in the current view hierarchy. Probably TextView is not a child of FrameLayout. So initialization fails leading to NullPointerException.
The other reason could be that you referenced wrong id.
Edit:
Id referenced in wrong
<TextView
android:id="#+id/textview_energyGreen_quantity"
Change this
textView = (TextView) fl.findViewById(R.id.textview_energyOrange_quantity);
to
textView = (TextView) fl.findViewById(R.id.textview_energyGreen_quantity);
Try this
private View mView;
mView =inflater.inflate(R.layout.item_energy_green_layout, null, false);
itemEnergyGreenQuantityTextView = itemEnergyGreen.getTextView(mView);

listview row in custom dialog invisible

I have a custom dialog with the format of Header + listview + footer. The corresponding code is as follows: (I don't mind to share the code but it is over 800 lines) ...
/* Setup dialog layout */
View header = (View)getLayoutInflater().inflate(R.layout.stockcountheader, null);
View footer = (View)getLayoutInflater().inflate(R.layout.stockcountfooter, null);
final Dialog listDialog;
listDialog = new Dialog(this, android.R.style.Theme_Dialog);
listDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater li = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.dialog, null, false);
listDialog.setContentView(v);
listDialog.setCancelable(true);
final AdditemAdapter adapter = new AdditemAdapter(this, R.layout.additemrow, updateitems);
final ListView filterrow = (ListView) listDialog.findViewById(R.id.dialoglist);
filterrow.addHeaderView(header);
filterrow.addFooterView(footer);
filterrow.setAdapter(adapter);
i = adapter.getCount();
listDialog.show();
In the header, I have a radiobutton and intend to control the visibility of the listview row. So, I have the following code: ...
radiostockresGroup = (RadioGroup)listDialog.findViewById(R.id.radiostockres);
radiostockresButton = (RadioButton) radiostockresGroup.findViewById(radiostockresGroup.getCheckedRadioButtonId());
radiostockresGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup radiostockresGroup, int checkedId) {
radiostockresButton = (RadioButton) radiostockresGroup.findViewById(checkedId);
switch(checkedId) {
case R.id.radioresno:
listDialog.findViewById(R.id.tblayout2).setVisibility(View.INVISIBLE);
break;
case R.id.radioresyes:
listDialog.findViewById(R.layout.tblayout2).setVisibility(View.VISIBLE);
break;
}
}
});
However, it give me the java null pointer error in the "listDialog.findViewById(R.id.tblayout2).setVisibility(View.INVISIBLE); " line. The xml for the listview and the dialog is as follows:
stocktakerow.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout android:id="#+id/tblayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0"
android:stretchColumns="*">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/txtfieldname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_span="3"
android:gravity="top" />
<EditText android:id="#+id/txtinput"
android:layout_toRightOf="#+id/txtfieldname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_span="3"
android:gravity="top" />
</TableRow>
</TableLayout>
</RelativeLayout>
I try to use " filterrow.findViewById(R.id.tblayout2).setVisibility(View.INVISIBLE);" and still got the same null pointer.
Please kindly give me a hand, thanks a lot in advance!
Kelvin
If you look at your code, you will notice:
final ListView filterrow = (ListView) listDialog.findViewById(R.id.dialoglist);
listDialog.findViewById(R.id.tblayout2).setVisibility(View.INVISIBLE);
Essentially, you are saying that R.id.tblayout2 is a member of the same thing as R.id.dialoglist. I don't think they are in this case.
Not seeing what your class extends, I can only suppose that this should work. If it doesn't, can you please post the class definitions? Just the line like public class MainActivity extends Activity { will do for both classes.
findViewById(R.id.tblayout2).setVisibility(View.INVISIBLE);

ClassNotFoundException android.view.scrollview when Inflating view

I'm trying to inflate a view containing a scrollview and i get a ClassNotFoundException android.view.scrollview when Inflating view on the following line:
View layout = inflater.inflate(R.layout.news_article, null, true);
I cannot find anything wrong myself and googling the issue didn't help me (unfortunatly).
On the other hand, this is actually also a workaround for something I don't know how to do.
Situation:
I've got a tab layout with 3 tabs. In each tab, I've got a listview containing a news-item. When I click on the news-item, I want the listview layout to be switched with the xml layout i'm now using for the popup (it's kinda cheating, but I don't know how to do it properly). So if anybody has a way to do this instead of using a popup, it will be the best answer for me.
Method where I inflate the layout:
#Override
public void onClick(View v)
{
//setContentView(R.layout.news_article);
final PopupWindow popUp;
LayoutInflater inflater = (LayoutInflater)NewsActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.news_article, null, false);
Display display =GetWindowManager().getDefaultDisplay();
int popUpWidth = display.getWidth();
int popUpHeight = display.getHeight();
popUp = new PopupWindow(layout, popUpWidth, popUpHeight, true);
popUp.setOutsideTouchable(true);
popUp.setTouchInterceptor(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
System.out.println("Touch Intercepted");
if(event.getAction() == MotionEvent.ACTION_OUTSIDE)
{
popUp.dismiss();
}
return false;
}
});
popUp.showAtLocation(getListView(), Gravity.TOP, 0, 75);
}
XML code for the layout:
<?xml version="1.0" encoding="utf-8"?>
<Scrollview
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/news_article_scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffffff">
<ImageView
android:id="#+id/news_article_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:src = "#drawable/ic_launcher"/>
<TextView
android:id="#+id/news_article_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#000000"
android:layout_toRightOf="#+id/news_article_icon"
android:layout_marginTop="10dp"
android:text="Header" />
<TextView
android:id="#+id/news_article_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/news_article_icon"
android:layout_marginTop="10dp"
android:textColor="#000000"
android:text="Text" />
</RelativeLayout>
</Scrollview>
*******EDIT********
Ok, the popup now shows, but I'm not receiving any events from it
I tried it in my code and it works when I change Scrollview to ScrollView in the xml file:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ScrollView>
The xml is correct if you write it with a small 'v' but the inflater does not recognize it and requires a capital 'V'.
You can see that very easily when you put a try catch block around it and check the exception because it says it there.
When you inflate your layout you set no parentView, but you set the flag true
View layout = inflater.inflate(R.layout.news_article, null, true);
Set the flag to false and add the view where you need it.
View layout = inflater.inflate(R.layout.news_article, null, false);
myParentViewGroup.add(layout);

Categories

Resources