Crash when calling findViewById

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Crash when calling findViewById

Postby chen8002004 » Tue Dec 15, 2009 5:56 am

Hi,

I am trying to add an image to EditText. I created a class derived from EditText and tryed to start the view. My application crash when calling findViewById. Can anyone help me about this problem? Many Thanks.

combobox.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/friendFilterInputTextR"
android:drawableLeft="@drawable/kaixin_btn_search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/friendFilterInputTextR"
/>
</RelativeLayout>

View that derived from EditText
public class FriendEditTextItem extends EditText implements OnKeyListener {
// the function interface for the parent classes
FilterFriendListener m_FilterFriendListener = null;


public FriendEditTextItem(Context context) {
super(context);
// Set KeyListener to ourself
this.setOnKeyListener(this);
}
public FriendEditTextItem(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// Set KeyListener to ourself
this.setOnKeyListener(this);
}
public FriendEditTextItem(Context context, AttributeSet attrs) {
super(context, attrs);
// Set KeyListener to ourself
this.setOnKeyListener(this);
}
// Allows the user to set an Listener and react to the event
public void setFilterFriendListener(FilterFriendListener listener) {
m_FilterFriendListener = listener;
}

Main java file
public class FriendListView extends ListActivity implements Runnable{

private ProgressDialog m_ProgressDialog = null;
private ArrayList<FriendInfo> m_frdatas = null;
private FriendAdapter m_adapter;
private FriendEditTextItem m_txtSearchFr;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.combobox);
// m_frdatas = new ArrayList<FriendInfo>();
Resources res = getResources();
Drawable img = res.getDrawable(R.drawable.kaixin_btn_search);
img.setBounds(0, 0, img.getIntrinsicWidth(), img.getIntrinsicHeight());

// Crash at this line..............
/////////////////////////////////////////////////////////////////
m_txtSearchFr = (FriendEditTextItem) findViewById(R.id.friendFilterInputTextR);
m_txtSearchFr.setCompoundDrawables(img, null, null, null);
chen8002004
Freshman
Freshman
 
Posts: 2
Joined: Tue Dec 15, 2009 5:45 am

Top

same problem

Postby brccabral » Mon Jan 25, 2010 12:26 am

Did you find a solution??

EditTextInput.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.mainApp;
  3.  
  4. import android.content.Context;
  5.  
  6. import android.widget.EditText;
  7.  
  8. import android.util.AttributeSet;
  9.  
  10. public class EditTextInput extends EditText {
  11.  
  12.         public String nome;
  13.  
  14.         public EditTextInput(Context context) {
  15.  
  16.                 super(context);
  17.  
  18.                 // TODO Auto-generated constructor stub
  19.  
  20.         }
  21.  
  22.  
  23.  
  24.         public EditTextInput(Context context, AttributeSet attrs) {
  25.  
  26.                 super(context, attrs);
  27.  
  28.                 // TODO Auto-generated constructor stub
  29.  
  30.         }
  31.  
  32.  
  33.  
  34.         public EditTextInput(Context context, AttributeSet attrs, int defStyle) {
  35.  
  36.                 super(context, attrs, defStyle);
  37.  
  38.                 // TODO Auto-generated constructor stub
  39.  
  40.         }
  41.  
  42. }
  43.  
  44.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

mainApp.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.mainApp;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.app.AlertDialog;
  9.  
  10. import android.os.Bundle;
  11.  
  12. import android.view.View;
  13.  
  14. import android.widget.Button;
  15.  
  16. import android.widget.EditText;
  17.  
  18. public class mainApp extends Activity {
  19.  
  20.         private EditTextInput input1;
  21.  
  22.        
  23.  
  24.     /* Called when the activity is first created. */
  25.  
  26.     @Override
  27.  
  28.     public void onCreate(Bundle savedInstanceState) {
  29.  
  30.         super.onCreate(savedInstanceState);
  31.  
  32.         setContentView(R.layout.main);
  33.  
  34.        
  35.  
  36.         //CRASH HERE
  37.  
  38.         input1 = (EditTextInput) findViewById(R.id.input1);
  39.  
  40.     }
  41.  
  42. }
  43.  
  44.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
Att,
Bruno Cabral
brccabral
Freshman
Freshman
 
Posts: 3
Joined: Sun Jan 24, 2010 11:55 pm

Postby chen8002004 » Mon Jan 25, 2010 1:40 pm

I have found the solution. The problem is in combobox.xml. If you define a class for a view, the class name should be written in the item of xml file.

in my case:
combobox.xml:
<com.kaixin001.view.FriendEditTextItem
android:id=....
android:layout_width=...
/>
chen8002004
Freshman
Freshman
 
Posts: 2
Joined: Tue Dec 15, 2009 5:45 am

THANKS

Postby brccabral » Tue Jan 26, 2010 1:32 am

That's it!
I changed my
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <EditText android:id="@+id/myid" .....></EditText>
Parsed in 0.000 seconds, using GeSHi 1.0.8.4
to
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <com.myapp.EditTextInput android:id="@+id/myid" .... />
Parsed in 0.000 seconds, using GeSHi 1.0.8.4
.

thanks
Att,
Bruno Cabral
brccabral
Freshman
Freshman
 
Posts: 3
Joined: Sun Jan 24, 2010 11:55 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 7 guests