fatal exception: main android - android

I am an android beginner, and I tryed to create an Employee Directory. But every time when I run the project I receive this error:
This is mai main.xml file code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation= "vertical" >
<LinearLayout
android:orientation="horizontal"
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/searchText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/search" />
</LinearLayout>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
And here is mai java clase :
public class EmployeeList extends ListActivity {
protected EditText searchText;
protected SQLiteDatabase db;
protected Cursor cursor;
protected ListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = (new DatabaseHelper(this)).getWritableDatabase();
searchText = (EditText)findViewById(R.id.searchText);
}
#SuppressWarnings("deprecation")
public void search(View view){
cursor = db.rawQuery("SELECT _id, firstName, lastName, title FROM employee WHERE firstName || ' ' || lastName LIKE ?",
new String[]{"%" + searchText.getText().toString() + "%"});
adapter = new SimpleCursorAdapter(this, R.layout.employee_list_item,
cursor, new String[]{"firstName", "lastName", "title"},
new int[] {R.id.firstName, R.id.lastName, R.id.title});
setListAdapter(adapter);
}
public void onListItemClick(ListView parent, View view, int position, long id) {
Intent intent = new Intent(this, EmployeeDetails.class);
Cursor cursor = (Cursor) adapter.getItem(position);
intent.putExtra("EMPLOYEE_ID", cursor.getInt(cursor.getColumnIndex("_id")));
startActivity(intent);
}
}
What is the problem, and whay i receive this error?

Change your list id to #android:id/list. ListActivity requires ListView to have such id.
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Related

Cant get id in custom listview

Main Activity
public class MainActivity extends AppCompatActivity {
Button b1,b2;
DBHelper mydb;
TextView id;
ListView listView;
SimpleCursorAdapter adapter;
String[] from = new String[] { mydb.ID,
mydb.NAME, mydb.ADDRESS };
int[] to = new int[] { R.id.id, R.id.name, R.id.address };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydb = new DBHelper(this);
Cursor cursor = mydb.fetch();
listView = (ListView) findViewById(R.id.listView);
adapter = new SimpleCursorAdapter(this, R.layout.list, cursor, from, to, 0);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
id=(TextView) findViewById(R.id.id);
int id_To_Search = Integer.valueOf(id.getText().toString());
Bundle dataBundle = new Bundle();
dataBundle.putInt("_id", id_To_Search);
Intent intent = new Intent(getApplicationContext(), AddPatient.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
Add class
if (extras != null) {
b2.setVisibility(View.INVISIBLE);
b3.setVisibility(View.VISIBLE);
b4.setVisibility(View.VISIBLE);
int Value = extras.getInt("_id");
if (Value > 0) {
Cursor rs = mydb.getData(Value);
id_To_Update = Value;
rs.moveToFirst();
name.setText(rs.getString(rs.getColumnIndex(DBHelper.NAME)));
address.setText(rs.getString(rs.getColumnIndex(DBHelper.ADDRESS)));
mobile.setText(rs.getString(rs.getColumnIndex(DBHelper.MOBILE)));
if(rs.getString(rs.getColumnIndex(DBHelper.GENDER)) != "female")
{
male.setChecked(true);
}
else
{
female.setChecked(true);
}
}
}
DBHelper
public Cursor getData(int id) {
SQLiteDatabase db = this.getReadableDatabase();
return db.rawQuery("select * from patients where _id=" + id + "", null);
}
Main.xml
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="65dp" />
list.xml
<TextView
android:id="#+id/id"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dp"
android:padding="3dp"
android:visibility="invisible" />
<TextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:padding="3dp"
android:fontFamily="sans-serif"
android:textSize="18sp"
android:layout_toEndOf="#+id/id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:padding="3dp"
android:visibility="visible"
android:layout_marginTop="25dp"
android:textSize="18sp"
android:layout_toEndOf="#+id/id"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
How can i get the id of the clicked list and show those details for edit in the next page.?How can i do it. Please help me. The next page is used to edit and delete purposes
You need to use the view to find view id for list elements.
Change below line in onItemClick()
id=(TextView) findViewById(R.id.id);
to
id=(TextView) arg1.findViewById(R.id.id);
you shouldn't rely on view id to access data.
you should have a separate model (let's say an ArrayList) containing the data shown in the listview. when the user select an item you grab the position of the item listed and pick the corresponding item in the array where you could find all information

NullPointerException error in my ListView

I have a fragment which I want to display a ListView
here's a portion of my code
public class ExpensesDaily extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
lvExpenses = (ListView) getActivity().findViewById(android.R.id.list);
Cursor expensesListCursor;
DbControl dbc = new DbControl(getActivity());
try {
dbc.open();
expensesListCursor = dbc.listExpenses();
String[] from = new String[] {"description", "cost" };
ListAdapter adapter = new SimpleCursorAdapter(getActivity(), R.layout.list_expenses, expensesListCursor, from, new int[] { R.id.tvDescription, R.id.tvCost },0);
lvExpenses.setAdapter(adapter);
}catch (SQLException e){
}
dbc.close();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.expenses_daily, container, false);
return rootView;
}
}
** DbControl class**
public class DbControl {
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
public DbControl(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public Cursor listExpenses(){
Cursor cursor;
cursor = database.query(MySQLiteHelper.TABLE_EXPENSES, new String[] {"id _id", "description, cost"}, "forDate='" + date + "'", null, null, null, "id asc");
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
expenses_daily.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/ll_expensesDaily"
android:baselineAligned="false">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</LinearLayout>
list_expenses.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="7dip">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:id="#+id/tvDescription" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:id="#+id/tvCost" />
</LinearLayout>
</LinearLayout>
but everytime I load the fragment, it will display this error
FATAL EXCEPTION: main java.lang.NullPointerException at com.code.imin.mymoneymanaged.ExpensesDaily.onCreate(ExpensesDaily.java:60)
which points to the line
lvExpenses.setAdapter(adapter) in class ExpensesDaily.
lvExpenses = (ListView) getActivity().findViewById(android.R.id.list);
You can't call getActivity().findViewById on onCreate because the view was not created yet. Change that logic to onActivityCreated

Android: SimpleCursorAdapter.ViewBinder, listview, error

I would like to display a two textView in my ListView depending on the database value.
But in result nothing is displayed (error)
This is my code:
public class ListbookActivity extends ListActivity implements OnQueryTextListener{
private DBHelper database;
private Intent intent;
private ListView listav;
private SimpleCursorAdapter adapter;
private String strclickmenu;
private Cursor cursor;
private Toast t;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] from = new String[] { "titolo", "autore" };
int[] to = new int[] { R.id.name, R.id.surname};
database = new DBHelper(getApplicationContext());
SQLiteDatabase db = database.getReadableDatabase();
//query
cursor = db.query("libreria", null, null, null, null, null, "titolo", null);
cursor.moveToFirst();
listav = (ListView) findViewById(R.id.list);
adapter = new SimpleCursorAdapter(this,R.layout.list_row, cursor,from,to,0);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
#Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId() == R.id.name) {
String userFullName = cursor.getString(cursor.getColumnIndex("titolo"));
TextView tit = (TextView)view;
tit.setText(userFullName);
return true;
} else if (view.getId() == R.id.surname) {
String userEmail = cursor.getString(cursor.getColumnIndex("titolo"));
TextView aut = (TextView)view;
aut.setText(userEmail);
return true;
} else {
return false;
}
}
});
listav.setAdapter(adapter);
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/name"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.51"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/surname"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.51"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
activity_listbook.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:smoothScrollbar="false"
android:fadingEdge="none"/>
</LinearLayout>
This is the error:
Unable to start activity componentInfo{com.example.mybooks.ListBookActivity} : Java.lang.nullPointerException

Android ListView: Can't add sms/inbox items to ListView using cursor

I am trying to call "body" and "address" from content://sms/inbox and add them to a ListView.
"address" going to the Item1 in the ListView, "body" going to SubItem in same ListView.
To do this, I am implementing a Cursor to add these items to the ListView.
When running the code how I have it now, I get no errors or crashes, but there is nothing added to my ListView at all. I have all of the permissions needed in the manifest.
Not sure where I went wrong :( But thanks in advance!
My XML layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/header__root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/black"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/burntorange"
android:orientation="horizontal" >
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="147dp"
android:layout_height="wrap_content"
android:layout_weight="0.13"
android:background="#color/burntorange"
android:padding="10dp"
android:text="Messages"
android:textSize="20dp" />
<Button
android:id="#+id/bNewMessage"
android:layout_width="120dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:layout_margin="4dp"
android:background="#color/gray"
android:gravity="center"
android:onClick="handleClick"
android:padding="4dp"
android:text="New Thread"
android:textColor="#color/white"
android:textSize="15dp" />
</LinearLayout>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
My Menu.java:
public class Menu extends ListActivity {
ListView itemList;
#SuppressWarnings("deprecation")
#Override
public void onCreate(Bundle biscuits) {
super.onCreate(biscuits);
setContentView(R.layout.header);
itemList = (ListView) findViewById(android.R.id.list);
Uri uri = Uri.parse("content://sms/inbox");
Cursor c = getContentResolver().query(uri, null, null, null, null);
startManagingCursor(c);
String[] number = new String [c.getCount()];
String[] body = new String [c.getCount()];
if(c.moveToFirst()){
for (int i=0; i<c.getCount();i++) {
body[i]=c.getString(c.getColumnIndexOrThrow("body")).toString();
number[i] =c.getString(c.getColumnIndexOrThrow("address")).toString();
c.moveToNext();
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
Map<String, String> datum = new HashMap<String, String>(2);
datum.put("contactNumber", number[i]);
datum.put("messagePreview", body[i]);
SimpleAdapter adapter = new SimpleAdapter(this, data,
android.R.layout.simple_list_item_1,
new String[] {"contactNumber", "messagePreview"},
new int [] {android.R.id.text1,
android.R.id.text2});
itemList.setAdapter(adapter);
}
}
}
public void handleClick(View v) {
Intent intent = new Intent();
intent.setClass(this, TextActivity.class);
startActivity(intent);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String cheese = (String) l.getItemAtPosition(position);
Toast.makeText(this, cheese + " Selected", Toast.LENGTH_SHORT).show();
try {
Class ourClass = Class.forName("com.example.mytexts." + cheese);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}

autocompletetextview not working

I have a contact list and when I want to make a search in my list I want to use an autocompletetext view but it is not working.
When I start typing in autocomplete text view nothing appears, but when I click on the search button it finds me the contact I want.
here is my .java:
public class Search extends ListActivity {
private static int[] TO = {R.id.rowid,R.id.name, R.id.mobilephone, R.id.email };
private static String[] FROM = {_ID,DbConstants.NAME, DbConstants.PHONE, DbConstants.EMAIL, };
private Button sButton;
private ListView lv1;
private static SQLiteDatabase db;
private DbCreate contacts;
private Cursor cursor;
private EditText searchText;
protected SimpleCursorAdapter adapter;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
searchText=(EditText)findViewById(R.id.searchtext);
sButton=(Button)findViewById(R.id.searchButton);
sButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
showDatabaseContent();
lv1 = getListView();
lv1.setTextFilterEnabled(true);
}
});
}
private Cursor getContacts() {
db = contacts.getReadableDatabase();
cursor = db.rawQuery("SELECT _id,name, phone, email FROM contactTest1 WHERE name LIKE ?",
new String[]{searchText.getText().toString()+"%"});
startManagingCursor(cursor);
return cursor;
}
public void showDatabaseContent(){
contacts = new DbCreate(this);
try {
cursor = getContacts();
showContacts(cursor);
} finally {
contacts.close();
db.close();
}
}
private void showContacts(Cursor cursor) {
//set up data binding
adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, FROM, TO);
setListAdapter(adapter);
}
public void onListItemClick(ListView parent, View v, int position, long id) {
Intent abaintent = new Intent(this,Detalii.class);
Cursor cursor = (Cursor) adapter.getItem(position);
abaintent.putExtra("Contact_ID", cursor.getInt(cursor.getColumnIndex("_id")));
startActivity(abaintent);
}
}
here is my search.xml:
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk//android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/searchtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="searchDefault" >
<requestFocus />
</AutoCompleteTextView>
<Button android:id="#+id/searchButton"
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Why do you have there
android:layout_weight="1"
?
I would remove it.

Categories

Resources