Here is my JAVA Code. When I run my app it crashes and i get following errors in my log
11-21 00:21:48.828: E/AndroidRuntime(349): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.geekosoft.contactsonline/com.geekosoft.contactsonline.AddContact}: java.lang.ClassCastException: android.widget.EditText
11-21 00:21:48.828: E/AndroidRuntime(349): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-21 00:21:48.828: E/AndroidRuntime(349): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-21 00:21:48.828: E/AndroidRuntime(349): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-21 00:21:48.828: E/AndroidRuntime(349): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-21 00:21:48.828: E/AndroidRuntime(349): at android.os.Handler.dispatchMessage(Handler.java:99)
11-21 00:21:48.828: E/AndroidRuntime(349): at android.os.Looper.loop(Looper.java:123)
11-21 00:21:48.828: E/AndroidRuntime(349): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-21 00:21:48.828: E/AndroidRuntime(349): at java.lang.reflect.Method.invokeNative(Native Method)
11-21 00:21:48.828: E/AndroidRuntime(349): at java.lang.reflect.Method.invoke(Method.java:507)
11-21 00:21:48.828: E/AndroidRuntime(349): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-21 00:21:48.828: E/AndroidRuntime(349): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-21 00:21:48.828: E/AndroidRuntime(349): at dalvik.system.NativeStart.main(Native Method)
11-21 00:21:48.828: E/AndroidRuntime(349): Caused by: java.lang.ClassCastException: android.widget.EditText
11-21 00:21:48.828: E/AndroidRuntime(349): at com.geekosoft.contactsonline.AddContact.initilizeEditContactInfo(AddContact.java:123)
11-21 00:21:48.828: E/AndroidRuntime(349): at com.geekosoft.contactsonline.AddContact.initilize(AddContact.java:68)
11-21 00:21:48.828: E/AndroidRuntime(349): at com.geekosoft.contactsonline.AddContact.onCreate(AddContact.java:42)
11-21 00:21:48.828: E/AndroidRuntime(349): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-21 00:21:48.828: E/AndroidRuntime(349): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
and here is my Java code
package com.geekosoft.contactsonline;
import java.util.ArrayList;
import java.util.Iterator;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableLayout;
public class AddContact extends Activity implements OnClickListener {
public static final String TAG = AddContact.class.getSimpleName();
ArrayList contactPhoneTypes, contactEmailTypes, contactIMTypes, contactAddressTypes;
String contactid;
int phoneNumbersIdStarter, emailIdStarter, imIdStarter, addressIdStarter;
LayoutInflater layoutInflater;
EditText name, number;
Spinner phoneType, emailType, addressType;
Button addPhoneField, addEmailField, addIMField, addAddressField;
TableLayout phoneFiledsCont, emailFiledsCont, imFiledsCont, addressFiledsCont;
#Override
protected void onCreate (Bundle contactsOnline) {
super.onCreate(contactsOnline);
setContentView(R.layout.add_contact);
initilize();
}
public void initilize () {
initilizeSpinnerTypes();
phoneNumbersIdStarter = 1000;
emailIdStarter = 2000;
imIdStarter = 3000;
addressIdStarter = 4000;
layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
name = (EditText) findViewById(R.id.etContactName);
addPhoneField = (Button) findViewById(R.id.bAddPhoneField);
addEmailField = (Button) findViewById(R.id.bAddEmailField);
addIMField = (Button) findViewById(R.id.bAddIMField);
addAddressField = (Button) findViewById(R.id.bAddaddressField);
phoneFiledsCont = (TableLayout) findViewById(R.id.tlPhoneFiledCont);
emailFiledsCont = (TableLayout) findViewById(R.id.tlEmailFiledsCont);
imFiledsCont = (TableLayout) findViewById(R.id.tlIMFiledsCont);
addressFiledsCont = (TableLayout) findViewById(R.id.tlAddressFiledsCont);
addPhoneField.setOnClickListener(this);
addEmailField.setOnClickListener(this);
addIMField.setOnClickListener(this);
addAddressField.setOnClickListener(this);
initilizeEditContactInfo();
}
public void initilizeSpinnerTypes() {
contactPhoneTypes = new ArrayList();
contactPhoneTypes.add(ContactsContract.CommonDataKinds.Phone.TYPE_HOME);
contactPhoneTypes.add(ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
contactPhoneTypes.add(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
contactPhoneTypes.add(ContactsContract.CommonDataKinds.Phone.TYPE_OTHER);
// contactPhoneTypes.add(ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM);
contactEmailTypes = new ArrayList();
contactEmailTypes.add(ContactsContract.CommonDataKinds.Email.TYPE_HOME);
contactEmailTypes.add(ContactsContract.CommonDataKinds.Email.TYPE_WORK);
contactEmailTypes.add(ContactsContract.CommonDataKinds.Email.TYPE_MOBILE);
contactEmailTypes.add(ContactsContract.CommonDataKinds.Email.TYPE_OTHER);
contactIMTypes = new ArrayList();
contactIMTypes.add(ContactsContract.CommonDataKinds.Im.TYPE_HOME);
contactIMTypes.add(ContactsContract.CommonDataKinds.Im.TYPE_WORK);
contactIMTypes.add(ContactsContract.CommonDataKinds.Im.TYPE_OTHER);
contactAddressTypes = new ArrayList();
contactAddressTypes.add(ContactsContract.CommonDataKinds.SipAddress.TYPE_HOME);
contactAddressTypes.add(ContactsContract.CommonDataKinds.SipAddress.TYPE_WORK);
contactAddressTypes.add(ContactsContract.CommonDataKinds.SipAddress.TYPE_OTHER);
}
public void initilizeEditContactInfo () {
contactid = getIntent().getStringExtra("contactId");
Log.d(TAG, "id = "+contactid);
Log.d(TAG, "query = "+ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
ContentResolver cr = getContentResolver();
Cursor contact = cr.query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone._ID+" = ?",new String[]{contactid}, "DISPLAY_NAME ASC");
// Cursor contacts = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
if (contact.getCount() > 0) {
contact.moveToFirst();
String name = contact.getString(contact.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.d(TAG, "name = "+name);
this.name.setText(name);
int loopCounter = 0;
if (Integer.parseInt(contact.getString(contact.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+" = ?",new String[]{contactid}, null);
while (phones.moveToNext()) {
loopCounter++;
int phoneType = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.d(TAG, "phone = "+phoneNumber);
addTextField("phone");
EditText phoneNumberField = (EditText) findViewById(8084+phoneNumbersIdStarter);
phoneNumberField.setText(phoneNumber);
Log.d("Spinner Getting", "Spinner Id = " + (8083+phoneNumbersIdStarter));
Spinner phoneNumberSpinner = (Spinner) findViewById(8083+phoneNumbersIdStarter);
Log.d("After Spinner", "phone counter = " + phoneNumbersIdStarter);
switch (phoneType) {
case Phone.TYPE_MOBILE:
// phoneNumberSpinner.setSelection(2, false);
break;
case Phone.TYPE_HOME:
// phoneNumberSpinner.setSelection(0, false);
String phoneTypesCodes = "home = "+Phone.TYPE_HOME+"mobile = "+Phone.TYPE_MOBILE+"work = "+Phone.TYPE_WORK
+"other = "+Phone.TYPE_OTHER;
Log.d(name + "(home number)", phoneNumber + phoneTypesCodes);
break;
case Phone.TYPE_WORK:
// phoneNumberSpinner.setSelection(1, false);
break;
case Phone.TYPE_OTHER:
// phoneNumberSpinner.setSelection(3, false);
break;
}
}
phones.close();
}
}
contact.close();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bAddPhoneField:
addTextField("phone");
break;
case R.id.bAddEmailField:
addTextField("email");
break;
case R.id.bAddIMField:
addTextField("im");
break;
case R.id.bAddaddressField:
addTextField("address");
break;
}
}
public void addTextField(String fieldType) {
// for text field code is 84
// for spinner code is 83
if (fieldType.equals("phone")) {
// for phone number code is 80
phoneNumbersIdStarter++;
View view = layoutInflater.inflate(R.layout.add_phone_row, null);
EditText phoneNumberField = (EditText) view.findViewById(R.id.etContactPhone);
Spinner phoneTypeSpinner = (Spinner) view.findViewById(R.id.sContactPhoneType);
Log.d(TAG, "spinner id = " + phoneTypeSpinner.getId());
phoneNumberField.setId(8084+phoneNumbersIdStarter);
phoneTypeSpinner.setId(8083+phoneNumbersIdStarter);
Log.d("Spinner Created", "Spinner Id = " + phoneTypeSpinner.getId());
setSpinnerValues(phoneTypeSpinner, "phone");
phoneFiledsCont.addView(view);
} else if (fieldType.equals("email")) {
// for email code is 69
emailIdStarter++;
View view = layoutInflater.inflate(R.layout.add_email_row, null);
EditText emailAddressField = (EditText) view.findViewById(R.id.etContactEmail);
Spinner emailTypeSpinner = (Spinner) view.findViewById(R.id.sContactEmailType);
emailAddressField.setId(6984+emailIdStarter);
emailTypeSpinner.setId(6983+emailIdStarter);
Log.d(TAG, "inside true");
setSpinnerValues(emailTypeSpinner, "email");
Log.d(TAG, "inside true 2");
emailFiledsCont.addView(view);
} else if (fieldType.equals("im")) {
// for address code is 65
imIdStarter++;
View view = layoutInflater.inflate(R.layout.add_im_row, null);
EditText imField = (EditText) view.findViewById(R.id.etContactIM);
Spinner imTypeSpinner = (Spinner) view.findViewById(R.id.sContactIMType);
imField.setId(6584+imIdStarter);
imTypeSpinner.setId(6583+imIdStarter);
setSpinnerValues(imTypeSpinner, "im");
imFiledsCont.addView(view);
} else if (fieldType.equals("address")) {
// for im code is 73
addressIdStarter++;
View view = layoutInflater.inflate(R.layout.add_address_row, null);
EditText addressField = (EditText) view.findViewById(R.id.etContactAddress);
Spinner addressTypeSpinner = (Spinner) view.findViewById(R.id.sContactAddressType);
addressField.setId(7384+addressIdStarter);
addressTypeSpinner.setId(7383+addressIdStarter);
setSpinnerValues(addressTypeSpinner, "address");
addressFiledsCont.addView(view);
}
}
public void setSpinnerValues (Spinner spinner, String spinnerType) {
ArrayAdapter spinnerAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Iterator iter = null;
if (spinnerType.equals("phone")) {
iter = contactPhoneTypes.iterator();
} else if (spinnerType.equals("email")) {
iter = contactEmailTypes.iterator();
} else if (spinnerType.equals("im")) {
iter = contactIMTypes.iterator();
} else if (spinnerType.equals("address")) {
iter = contactAddressTypes.iterator();
}
while (iter.hasNext()) {
if (spinnerType.equals("phone")) {
spinnerAdapter.add(ContactsContract.CommonDataKinds.Phone.getTypeLabel(this.getResources(), iter.next(), getString(R.string.undefinedTypeLabel)).toString());
} else if (spinnerType.equals("email")) {
spinnerAdapter.add(ContactsContract.CommonDataKinds.Email.getTypeLabel(this.getResources(), iter.next(), getString(R.string.undefinedTypeLabel)).toString());
} else if (spinnerType.equals("im")) {
spinnerAdapter.add(ContactsContract.CommonDataKinds.Im.getTypeLabel(this.getResources(), iter.next(), getString(R.string.undefinedTypeLabel)).toString());
} else if (spinnerType.equals("address")) {
spinnerAdapter.add(ContactsContract.CommonDataKinds.Im.getTypeLabel(this.getResources(), iter.next(), getString(R.string.undefinedTypeLabel)).toString());
}
}
spinner.setAdapter(spinnerAdapter);
spinner.setPrompt(getString(R.string.selectLabel));
}
}
here is my add_contact.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TableLayout
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TableRow>
<TextView android:text="Target Account"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<Spinner android:id="#+id/accountSpinner"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:layout_weight="1" />
</TableRow>
<TableRow>
<TextView android:text="Contact Name"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<EditText android:id="#+id/etContactName"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_weight="1"/>
</TableRow>
<TableRow>
<TextView android:text="Contact Phone"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<Button android:id="#+id/bAddPhoneField"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Add Field"/>
</TableRow>
</TableLayout>
<TableLayout android:id="#+id/tlPhoneFiledCont"
android:layout_width="fill_parent" android:layout_height="wrap_content">
</TableLayout>
<TableLayout
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TableRow>
<TextView android:text="Contact Email"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
<Button android:id="#+id/bAddEmailField"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Add Field"/>
<TableLayout android:id="#+id/tlEmailFiledsCont"
android:layout_width="fill_parent" android:layout_height="wrap_content">
</TableLayout>
<TextView android:text="Contact IM"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<Button android:id="#+id/bAddIMField"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Add Field"/>
<TableLayout android:id="#+id/tlIMFiledsCont"
android:layout_width="fill_parent" android:layout_height="wrap_content">
</TableLayout>
<TextView android:text="Contact Address"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<Button android:id="#+id/bAddaddressField"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Add Field"/>
<TableLayout android:id="#+id/tlAddressFiledsCont"
android:layout_width="fill_parent" android:layout_height="wrap_content">
</TableLayout>
<TableLayout
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TableRow>
<Button android:text="#string/save"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="#+id/contactSaveButton" android:layout_weight="1"/>
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
and here is my add_phone_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent" >
<EditText android:id="#+id/etContactPhone"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:inputType="phone" />
<Spinner android:id="#+id/sContactPhoneType"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</TableRow>
I am creating Spinner and EditText dynamically in bellow fucntion
if (fieldType.equals("phone")) {
// for phone number code is 80
phoneNumbersIdStarter++;
View view = layoutInflater.inflate(R.layout.add_phone_row, null);
EditText phoneNumberField = (EditText) view.findViewById(R.id.etContactPhone);
Spinner phoneTypeSpinner = (Spinner) view.findViewById(R.id.sContactPhoneType);
Log.d(TAG, "spinner id = " + phoneTypeSpinner.getId());
phoneNumberField.setId(8084+phoneNumbersIdStarter);
phoneTypeSpinner.setId(8083+phoneNumbersIdStarter);
Log.d("Spinner Created", "Spinner Id = " + phoneTypeSpinner.getId());
setSpinnerValues(phoneTypeSpinner, "phone");
phoneFiledsCont.addView(view);
}
and getting their values like below lines
EditText phoneNumberField = (EditText) findViewById(8084+phoneNumbersIdStarter);
Spinner phoneNumberSpinner = (Spinner) findViewById(8083+phoneNumbersIdStarter);
and each time i add these fields also add 1 in phoneNumbersIdStarter
if I comment the following line in my code it runs great
Spinner phoneNumberSpinner = (Spinner) findViewById(8083+phoneNumbersIdStarter);
Do you have some specific reason to not access the widgets using R.id.?
I don't think a good idea to use "8083+phoneNumbersIdStarter", because the int id which is generated in class R will change in each compilation, so probably the int 8083+phoneNumbersIdStarter is referring to a widget which is not an EditText.
So, change to something like this:
Spinner phoneNumberSpinner = (Spinner) findViewById(R.id.your_spinner_id);
I found error by myself. Error was because of id confliction. Because I was adding 1 in phoneNumbersIdStarter on click of Add Phone Button. So my EditText ID starer was 8084+phoneNumbersIdStarter and Spinner ID starter 8083+phoneNumbersIdStarter thats why Spinnner was conflicting with EditText when creating fields second time.
Solved error by changing Spinner ID starter from 8083+phoneNumbersIdStarter to 9083+phoneNumbersIdStarter as shown below now it is working great.
EditText phoneNumberField = (EditText) findViewById(8084+phoneNumbersIdStarter);
Spinner phoneNumberSpinner = (Spinner) findViewById(9083+phoneNumbersIdStarter);
Related
I want that when I click on an element of GridView displayed letter, which I clicked. But initially I got here error when I wanted to take the position of the element. If I understood correctly, gridview does not see the textview, but i don't know why.
Error:
5312-5312/standandroid.ru.words E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.content.res.Resources.getText(Resources.java:240)
at android.content.res.MiuiResources.getText(MiuiResources.java:131)
at android.widget.Toast.makeText(Toast.java:273)
at standandroid.ru.words.MainActivity$1$1.onItemClick(MainActivity.java:70)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1130)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2818)
at android.widget.AbsListView$1.run(AbsListView.java:3498)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5137)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:756)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:572)
at miui.dexspy.DexspyInstaller.main(DexspyInstaller.java:171)
at dalvik.system.NativeStart.main(Native Method)
MainActivity.java :
public class MainActivity extends Activity {
TextView txt, textTV;
EditText etxt;
Button btn;
ArrayAdapter<String> adapter;
GridView gridView;
String name;
char c;
ArrayList arrayList=new ArrayList();
int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView) findViewById(R.id.textView);
etxt = (EditText) findViewById(R.id.editText);
btn = (Button) findViewById(R.id.button);
textTV = (TextView) findViewById(R.id.tv);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
name = etxt.getText().toString();
TreeSet list = new TreeSet();
for (i = 0; i < name.length(); i++) {
c = name.charAt(i);
list.add(c);
}
arrayList = new ArrayList(list);
adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.item, R.id.tv, arrayList);
gridView = (GridView) findViewById(R.id.gridmain);
gridView.setAdapter(adapter);
ViewGrid();
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, position,Toast.LENGTH_LONG).show();
}
});
}
});}
private void ViewGrid(){
gridView.setNumColumns(4);
gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH); }}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/textView"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="175dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:singleLine="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="48dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp" />
<GridView
android:id="#+id/gridmain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:layout_alignTop="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"></GridView>
</RelativeLayout>
item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tv"
android:textSize="40sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
http://i.stack.imgur.com/j5S93.png
Ohh! I see, the issue is that Toast is thinking of position as a resourceID. If you see Toast specs here, you would see that you can define resourceId or a CharSequence. So make sure you are passing the position as a String. Something like:
Toast.makeText(MainActivity.this, String.valueOf(position),Toast.LENGTH_LONG).show();
I was working on a GUI inside of my app. I originally had a crappy look to it that just contained all of the basic stuff to test my java functions in. After I had all the functions up and running I redisgned the GUI using the graphical editor in Eclipse. The next time I ran my app I had the android.webkit.WebView cannot be cast to android.widget.Button in my LogCat. I didn't change anything in the java and all the declarations in the GUI are the same as far as I can tell. I can easily revert the GUI back to the crappy version but that doesn't help me much. I hope someone on here can help me find my problem. I have posted the xml, java, and LogCat.
05-09 13:25:19.756: E/AndroidRuntime(10424): FATAL EXCEPTION: main
05-09 13:25:19.756: E/AndroidRuntime(10424): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dcc/com.example.dcc.ActionItem}:
java.lang.ClassCastException: android.webkit.WebView cannot be cast to android.widget.Button
05-09 13:25:19.756: E/AndroidRuntime(10424): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
05-09 13:25:19.756: E/AndroidRuntime(10424): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05-09 13:25:19.756: E/AndroidRuntime(10424): at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-09 13:25:19.756: E/AndroidRuntime(10424): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-09 13:25:19.756: E/AndroidRuntime(10424): at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 13:25:19.756: E/AndroidRuntime(10424): at android.os.Looper.loop(Looper.java:137)
05-09 13:25:19.756: E/AndroidRuntime(10424): at android.app.ActivityThread.main(ActivityThread.java:4745)
05-09 13:25:19.756: E/AndroidRuntime(10424): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 13:25:19.756: E/AndroidRuntime(10424): at java.lang.reflect.Method.invoke(Method.java:511)
05-09 13:25:19.756: E/AndroidRuntime(10424): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-09 13:25:19.756: E/AndroidRuntime(10424): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-09 13:25:19.756: E/AndroidRuntime(10424): at dalvik.system.NativeStart.main(Native Method)
05-09 13:25:19.756: E/AndroidRuntime(10424): Caused by: java.lang.ClassCastException: android.webkit.WebView cannot be cast to android.widget.Button
05-09 13:25:19.756: E/AndroidRuntime(10424): at com.example.dcc.ActionItem.onCreate(ActionItem.java:54)
05-09 13:25:19.756: E/AndroidRuntime(10424): at android.app.Activity.performCreate(Activity.java:5008)
05-09 13:25:19.756: E/AndroidRuntime(10424): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-09 13:25:19.756: E/AndroidRuntime(10424): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
05-09 13:25:19.756: E/AndroidRuntime(10424): ... 11 more
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="4" >
<LinearLayout
android:layout_width="212dp"
android:layout_height="match_parent"
android:layout_column="0"
android:layout_columnSpan="3"
android:layout_gravity="left"
android:layout_row="0"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Button1_name"
android:drawableRight="#drawable/news"/>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Button2_name" />
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Button3_name"
android:drawableRight="#drawable/cal"/>
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Button4_name"
android:drawableRight="#drawable/mail"/>
<Button
android:id="#+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Button5_name"
android:drawableRight="#drawable/gallery" />
<Button
android:id="#+id/button6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Button6_name"
android:drawableRight="#drawable/report" />
<Button
android:id="#+id/button7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Button7_name"
android:drawableRight="#drawable/ep" />
<Button
android:id="#+id/button8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/Button8_name"
android:drawableRight="#drawable/directory" />
</LinearLayout>
<GridLayout
android:id="#+id/a"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_column="1"
android:layout_columnSpan="2"
android:layout_gravity="left"
android:layout_row="0"
android:columnCount="2" >
</GridLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="10dp"
android:layout_height="fill_parent"
android:layout_column="2"
android:layout_gravity="left"
android:layout_row="0"
android:background="#drawable/black"
tools:ignore="ContentDescription" />
 
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="238dp"
android:layout_height="fill_parent"
android:layout_column="3"
android:layout_gravity="left|top"
android:layout_row="0"
android:background="#android:color/black" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/feedtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/feeddescribtion"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/feedpubdate"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/feedlink"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="web" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="594dp" >
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="waiting" />
</LinearLayout>
</ScrollView>
<WebView
android:id="#+id/webView1"
android:layout_width="830dp"
android:layout_height="555dp"
android:layout_column="3"
android:layout_gravity="right|bottom"
android:layout_row="0" />
<Button
android:id="#+id/email_button1"
android:layout_width="585dp"
android:layout_height="114dp"
android:layout_column="3"
android:layout_gravity="center_horizontal|top"
android:layout_row="0"
android:text="Submit Action Item Now" />
<TextView
android:id="#+id/actionView"
android:layout_width="243dp"
android:layout_height="113dp"
android:layout_column="3"
android:layout_gravity="right|top"
android:layout_row="0"
android:text="Loading" />
</GridLayout>
package com.example.dcc;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class ActionItem extends ListActivity implements OnClickListener {
// Create buttons Globally so they are available to all methods
public static RSSFeed myRssFeed = null;
private Button btnEmail = null;
static WebView webView;
Button newsB;
Button loginB;
Button calB;
Button mailB;
Button photoB;
Button reportB;
Button actionB;
Button directoryB;
String currentItem;
TextView thisAction;
int currentLocation;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.action_item);
btnEmail = (Button) findViewById(R.id.email_button1);
newsB = (Button) findViewById(R.id.button1);
loginB = (Button) findViewById(R.id.button2);
calB = (Button) findViewById(R.id.button3);
mailB = (Button) findViewById(R.id.button4);
photoB = (Button) findViewById(R.id.button5);
reportB = (Button) findViewById(R.id.button6);
actionB = (Button) findViewById(R.id.button7);
directoryB = (Button) findViewById(R.id.button8);
thisAction = (TextView) findViewById(R.id.actionView);
newsB.setOnClickListener(this);
loginB.setOnClickListener(this);
calB.setOnClickListener(this);
mailB.setOnClickListener(this);
photoB.setOnClickListener(this);
reportB.setOnClickListener(this);
actionB.setOnClickListener(this);
directoryB.setOnClickListener(this);
btnEmail.setOnClickListener(this);
new MyTask().execute();
// this portion of the onCreate is dedicated to running the webView
// another webpage: http://creflodollarministries.org/
// main webpage: http://www.virtualDiscoveryCenter.net
webView = (WebView) findViewById(R.id.webView1);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://www.virtualDiscoveryCenter.net");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
Uri data = getIntent().getData();
if (savedInstanceState == null) {
String url = getIntent().getDataString();
// do something with this URL.
webView.loadUrl(url);
}
}
private class MyTask extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... arg0) {
try {
URL rssUrl = new URL(
"http://www.virtualdiscoverycenter.net/feed/");// http://www.virtualdiscoverycenter.net/feed/
SAXParserFactory mySAXParserFactory = SAXParserFactory
.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(rssUrl.openStream());
myXMLReader.parse(myInputSource);
myRssFeed = myRSSHandler.getFeed();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
if (myRssFeed != null) {
TextView feedTitle = (TextView) findViewById(R.id.feedtitle);
TextView feedDescribtion = (TextView) findViewById(R.id.feeddescribtion);
TextView feedPubdate = (TextView) findViewById(R.id.feedpubdate);
TextView feedLink = (TextView) findViewById(R.id.feedlink);
feedTitle.setText(myRssFeed.getTitle());
feedDescribtion.setText(myRssFeed.getDescription());
feedPubdate.setText(myRssFeed.getPubdate());
feedLink.setText(myRssFeed.getLink());
ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(
getApplicationContext(),
android.R.layout.simple_list_item_1,
myRssFeed.getList());
setListAdapter(adapter);
thisAction.setText(myRssFeed.getItem(0).getTitle());
} else {
TextView textEmpty = (TextView) findViewById(android.R.id.empty);
textEmpty.setText("No Feed Found!");
}
super.onPostExecute(result);
}
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// Uri feedUri = Uri.parse(myRssFeed.getItem(position).getLink());
// Intent myIntent = new Intent(Intent.ACTION_VIEW, feedUri);
// startActivity(myIntent);
String url = myRssFeed.getItem(position).getLink();
currentItem = myRssFeed.getItem(position).getTitle();
currentLocation = position;
thisAction.setText(currentItem);
webView.loadUrl(url);
}
public void sendEmail(View v) {
// The following code is the implementation of Email client
Intent i = new Intent(android.content.Intent.ACTION_SEND);
i.setType("text/plain");
String[] address = { "bevins2012#hotmail.com" };
i.putExtra(android.content.Intent.EXTRA_EMAIL, address);
i.putExtra(android.content.Intent.EXTRA_SUBJECT, myRssFeed.getItem(currentLocation).getDescription());
i.putExtra(android.content.Intent.EXTRA_TEXT,
"Action Item from DCC app.");
startActivityForResult((Intent.createChooser(i, "Email")), 1);
}
public void onClick(View v) {
// this switch listens for any and all click actions in the app
// each case is a button in the menu.
switch (v.getId()) {
case R.id.button1:
startActivity(new Intent(this, AndroidRssReader.class));
break;
case R.id.button2:
startActivity(new Intent(this, MainActivity.class));
break;
case R.id.button3:
startActivity(new Intent(this, MainActivity.class));
break;
case R.id.button4:
startActivity(new Intent(this, EmailMain.class));
break;
case R.id.button5:
startActivity(new Intent(this, CustomizedListView.class));
break;
case R.id.button6:
startActivity(new Intent(this, LaunchActivity.class));
break;
case R.id.button7:
startActivity(new Intent(this, ActionItem.class));
break;
case R.id.button8:
startActivity(new Intent(this, MainActivity.class));
finish();
break;
case R.id.email_button1:
// Calling sendEmail from the activity class
sendEmail(v);
break;
}
}
}
Perhaps the auto-generated R class is messed up. Try to clean and rebuild your project by using Project > Clean... in Eclipse.
I'm a bit new to StackOverflow and I was wondering if I could have a bit of input on the following issue: I'm attempting to save a bit of data in a sqlite database but each time I attempt to do so the app force closes and I'm not sure why. I've cleaned the file and there are no issues. (I have no idea what I've done wrong - but something has been programmed incorrectly.)
JAVA:
import android.app.Activity;
import android.app.AlertDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TimePicker;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.widget.TimePicker;
import com.nfc.linkingmanager.TimePickerFragment.TimePickedListener;
import java.util.Calendar;
public class AddEditCountry extends Activity implements TimePickedListener
{
private TextView mPickedTimeText;
private Button mPickTimeButton;
private long rowID;
private EditText nameEt;
private EditText capEt;
private EditText codeEt;
private TimePicker timeEt;
public static final String KEY_BUNDLE_TIME = "time";
public static final String KEY_BUNDLE_MIN = "min";
#Override
public void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.add_country); // where your_layout is the name of the xml file with the layout you want to use minus the .xml extention
//this layout must contain all of these views below that you are trying to initialize
nameEt = (EditText) findViewById(R.id.nameEdit);
capEt = (EditText) findViewById(R.id.capEdit);
codeEt = (EditText) findViewById(R.id.codeEdit);
timeEt = (TimePicker) findViewById(R.id.timeEdit);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
rowID = extras.getLong("row_id");
nameEt.setText(extras.getString("name"));
capEt.setText(extras.getString("cap"));
codeEt.setText(extras.getString("code"));
String time = extras.getString("time");
String[] parts = time.split(":");
timeEt.setCurrentHour(Integer.valueOf(parts[0]));
timeEt.setCurrentMinute(Integer.valueOf(parts[1]));
timeEt.setIs24HourView(false);
}
Button saveButton =(Button) findViewById(R.id.saveBtn);
saveButton.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
if (nameEt.getText().length() != 0)
{
AsyncTask<Object, Object, Object> saveContactTask =
new AsyncTask<Object, Object, Object>()
{
#Override
protected Object doInBackground(Object... params)
{
saveContact();
return null;
}
#Override
protected void onPostExecute(Object result)
{
finish();
}
};
saveContactTask.execute((Object[]) null);
}
else
{
AlertDialog.Builder alert = new AlertDialog.Builder(AddEditCountry.this);
alert.setTitle(R.string.errorTitle);
alert.setMessage(R.string.errorMessage);
alert.setPositiveButton(R.string.errorButton, null);
alert.show();
}
}
});
}
private void saveContact()
{
DatabaseConnector dbConnector = new DatabaseConnector(this);
if (getIntent().getExtras() == null)
{
dbConnector.insertContact(nameEt.getText().toString(),
capEt.getText().toString(),
timeEt.getCurrentHour().toString() + ":"
+ timeEt.getCurrentMinute().toString(),
codeEt.getText().toString());
}
else
{
dbConnector.insertContact(nameEt.getText().toString(),
capEt.getText().toString(),
timeEt.getCurrentHour().toString() + ":"
+ timeEt.getCurrentMinute().toString(),
codeEt.getText().toString());
}
}
#Override
public void onTimePicked(Calendar time)
{
// display the selected time in the TextView
mPickedTimeText.setText(DateFormat.format("h:mm a", time));
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1">
<LinearLayout android:id="#+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<EditText android:id="#+id/nameEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:hint="#string/name_hint"
android:inputType="textPersonName|textCapWords"/>
<EditText android:id="#+id/capEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:hint="#string/cap_hint"
android:inputType="textPersonName|textCapWords"/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data Limit"
android:textColor="#ffffff"
android:textAppearance="?android:textAppearanceMedium" />
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="left"
android:textColor="#ffffff"
android:text="10MB" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="right"
android:textColor="#ffffff"
android:text="Unlimited Data" />
</LinearLayout>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bandwidth Limit"
android:textColor="#ffffff"
android:textAppearance="?android:textAppearanceMedium" />
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="left"
android:textColor="#ffffff"
android:text="10kbs" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:textColor="#ffffff"
android:gravity="right"
android:text="Unlimited Bandwidth" />
</LinearLayout>
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceSmall" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WiFi Time Limit"
android:textColor="#ffffff"
android:textAppearance="?android:textAppearanceMedium" />
<TimePicker
android:id="#+id/timeEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1.0" />
<EditText
android:id="#+id/codeEdit"
android:inputType="textUri"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:lines="1"
android:hint="#string/code_hint"
android:imeOptions="actionNext" />
<Button android:id="#+id/saveBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_gravity="center_horizontal"
android:text="#string/save_btn"/>
</LinearLayout>
</ScrollView>
PROBLEMS:
Cleaned - No Problems
LOGCAT:
03-29 13:23:28.950: D/OpenGLRenderer(20744): Enabling debug mode 0
03-29 13:23:33.780: D/AndroidRuntime(20744): Shutting down VM
03-29 13:23:33.780: W/dalvikvm(20744): threadid=1: thread exiting with uncaught exception (group=0x41f7b930)
03-29 13:23:33.790: E/AndroidRuntime(20744): FATAL EXCEPTION: main
03-29 13:23:33.790: E/AndroidRuntime(20744): android.app.SuperNotCalledException: Activity {com.app.gamedemo/com.app.gamedemo.AddEditCountry} did not call through to super.onCreate()
03-29 13:23:33.790: E/AndroidRuntime(20744): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2146)
03-29 13:23:33.790: E/AndroidRuntime(20744): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-29 13:23:33.790: E/AndroidRuntime(20744): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-29 13:23:33.790: E/AndroidRuntime(20744): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-29 13:23:33.790: E/AndroidRuntime(20744): at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 13:23:33.790: E/AndroidRuntime(20744): at android.os.Looper.loop(Looper.java:137)
03-29 13:23:33.790: E/AndroidRuntime(20744): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-29 13:23:33.790: E/AndroidRuntime(20744): at java.lang.reflect.Method.invokeNative(Native Method)
03-29 13:23:33.790: E/AndroidRuntime(20744): at java.lang.reflect.Method.invoke(Method.java:511)
03-29 13:23:33.790: E/AndroidRuntime(20744): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-29 13:23:33.790: E/AndroidRuntime(20744): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-29 13:23:33.790: E/AndroidRuntime(20744): at dalvik.system.NativeStart.main(Native Method)
03-29 13:23:35.390: I/Process(20744): Sending signal. PID: 20744 SIG: 9
The problem is here in your onCreate()
#Override
public void onCreate(Bundle savedInstanceState)
{
nameEt = (EditText) findViewById(R.id.nameEdit);
capEt = (EditText) findViewById(R.id.capEdit);
codeEt = (EditText) findViewById(R.id.codeEdit);
timeEt = (TimePicker) findViewById(R.id.timeEdit);
You need to call setContentView() before trying to access your EditTexts and so on. Your Views "live" within your layout (xml file) so they are null until you inflate the layout. Change it to someting like
#Override
public void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.your_layout); // where your_layout is the name of the xml file with the layout you want to use minus the .xml extention
//this layout must contain all of these views below that you are trying to initialize
nameEt = (EditText) findViewById(R.id.nameEdit);
capEt = (EditText) findViewById(R.id.capEdit);
codeEt = (EditText) findViewById(R.id.codeEdit);
timeEt = (TimePicker) findViewById(R.id.timeEdit);
Edit
Just like the error says, you are trying to cast a TextView to a TimePicker. Here
timeEt = (TimePicker) findViewById(R.id.timeEdit);
(R.id.timeEdit) points to a TextView but here
private TimePicker timeEt;
you declare it as a TimePicker. If you want it to be a TimePicker then change it in your xml
`<TimePicker
android:id="#+id/timeEdit"
style="#style/StyleText"/> `
Also, you have multiple ids in your xml
android:id="#+id/codeText"
might want to change that
Second Edit
03-29 13:23:33.790: E/AndroidRuntime(20744): android.app.SuperNotCalledException: Activity {com.app.gamedemo/com.app.gamedemo.AddEditCountry} did not call through to super.onCreate()
This line in the logcat says it all. You didn't call super.onCreate(). Add the following line as the first line in your onCreate() method
super.onCreate(savedInstanceState);
When reading your logcat, look for where it says Fatal Exception. The next line should say what it is (Null Pointer Exception, Class Cast Exception, etc...) then look for the first line that references your package name and it will tell you where to start looking for the error. Ex. Main.java 57 tells you that whatever the fatal exception was occurs in your Main.java file at line 57. The problem may originally come from somewhere else but this is always a good place to start so you can narrow it down and post relevant code if you need help.
I have some XML which should be displaying the hour from TimePicker data but every time I try to view the data it force closes. I've narrowed the issue down to an incorrect setup in my XML file(s) but I can't seem to correct the issue and keep the app from force closing.
view_country xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:layout_margin="5dp">
<TableRow>
<TextView
style="#style/StyleLabel"
android:text="#string/name_lbl"/>
<TextView
android:id="#+id/nameText"
style="#style/StyleText"/>
</TableRow>
<TableRow>
<TextView
style="#style/StyleLabel"
android:text="#string/cap_lbl"/>
<TextView
android:id="#+id/capText"
style="#style/StyleText"/>
</TableRow>
<TableRow>
<TextView
style="#style/StyleLabel"
android:text="#string/code_lbl"/>
<TextView
android:id="#+id/codeText"
style="#style/StyleText"/>
</TableRow>
<TableRow>
<TextView
style="#style/StyleLabel"
android:text="Linked Users"/>
<TextView
style="#style/StyleText"/>
</TableRow>
<TableRow>
<TextView
style="#style/StyleLabel"
android:text="Time Limit"/>
<TextView
android:id="#+id/timeEdit"
style="#style/StyleText"/>
</TableRow>
</TableLayout>
ViewCountry java
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.TimePicker;
public class ViewCountry extends Activity {
private long rowID;
private TextView nameTv;
private TextView capTv;
private TextView codeTv;
private TextView timeTv;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.view_country);
setUpViews();
Bundle extras = getIntent().getExtras();
rowID = extras.getLong(CountryList.ROW_ID);
}
private void setUpViews() {
nameTv = (TextView) findViewById(R.id.nameText);
capTv = (TextView) findViewById(R.id.capText);
timeTv = (TextView) findViewById(R.id.timeEdit);
codeTv = (TextView) findViewById(R.id.codeText);
}
#Override
protected void onResume()
{
super.onResume();
new LoadContacts().execute(rowID);
}
private class LoadContacts extends AsyncTask<Long, Object, Cursor>
{
DatabaseConnector dbConnector = new DatabaseConnector(ViewCountry.this);
#Override
protected Cursor doInBackground(Long... params)
{
dbConnector.open();
return dbConnector.getOneContact(params[0]);
}
#Override
protected void onPostExecute(Cursor result)
{
super.onPostExecute(result);
result.moveToFirst();
// get the column index for each data item
int nameIndex = result.getColumnIndex("name");
int capIndex = result.getColumnIndex("cap");
int codeIndex = result.getColumnIndex("code");
int timeIndex = result.getColumnIndex("time");
nameTv.setText(result.getString(nameIndex));
capTv.setText(result.getString(capIndex));
timeTv.setText(result.getInt(timeIndex));
codeTv.setText(result.getString(codeIndex));
result.close();
dbConnector.close();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.view_country_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.editItem:
Intent addEditContact =
new Intent(this, AddEditCountry.class);
addEditContact.putExtra(CountryList.ROW_ID, rowID);
addEditContact.putExtra("name", nameTv.getText());
addEditContact.putExtra("cap", capTv.getText());
addEditContact.putExtra("code", codeTv.getText());
startActivity(addEditContact);
return true;
case R.id.deleteItem:
deleteContact();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void deleteContact()
{
AlertDialog.Builder alert = new AlertDialog.Builder(ViewCountry.this);
alert.setTitle(R.string.confirmTitle);
alert.setMessage(R.string.confirmMessage);
alert.setPositiveButton(R.string.delete_btn,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int button)
{
final DatabaseConnector dbConnector =
new DatabaseConnector(ViewCountry.this);
AsyncTask<Long, Object, Object> deleteTask =
new AsyncTask<Long, Object, Object>()
{
#Override
protected Object doInBackground(Long... params)
{
dbConnector.deleteContact(params[0]);
return null;
}
#Override
protected void onPostExecute(Object result)
{
finish();
}
};
deleteTask.execute(new Long[] { rowID });
}
}
);
alert.setNegativeButton(R.string.cancel_btn, null).show();
}
}
Database Connector
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class DatabaseConnector {
private static final String DB_NAME = "WorldCountries";
private SQLiteDatabase database;
private DatabaseOpenHelper dbOpenHelper;
public DatabaseConnector(Context context) {
dbOpenHelper = new DatabaseOpenHelper(context, DB_NAME, null, 1);
}
public void open() throws SQLException
{
//open database in reading/writing mode
database = dbOpenHelper.getWritableDatabase();
}
public void close()
{
if (database != null)
database.close();
}
public void insertContact(String name, String cap, String code, String time)
{
ContentValues newCon = new ContentValues();
newCon.put("name", name);
newCon.put("cap", cap);
newCon.put("time", time);
newCon.put("code", code);
open();
database.insert("country", null, newCon);
close();
}
public void updateContact(long id, String name, String cap,String code, String time)
{
ContentValues editCon = new ContentValues();
editCon.put("name", name);
editCon.put("cap", cap);
editCon.put("time", time);
editCon.put("code", code);
open();
database.update("country", editCon, "_id=" + id, null);
close();
}
public Cursor getAllContacts()
{
return database.query("country", new String[] {"_id", "name"},
null, null, null, null, "name");
}
public Cursor getOneContact(long id)
{
return database.query("country", null, "_id=" + id, null, null, null, null);
}
public void deleteContact(long id)
{
open();
database.delete("country", "_id=" + id, null);
close();
}
}
AddEditCountry java
import android.app.Activity;
import android.app.AlertDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TimePicker;
public class AddEditCountry extends Activity {
private long rowID;
private EditText nameEt;
private EditText capEt;
private EditText codeEt;
private TimePicker timeEt;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.add_country);
nameEt = (EditText) findViewById(R.id.nameEdit);
capEt = (EditText) findViewById(R.id.capEdit);
codeEt = (EditText) findViewById(R.id.codeEdit);
timeEt = (TimePicker) findViewById(R.id.timeEdit);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
rowID = extras.getLong("row_id");
nameEt.setText(extras.getString("name"));
capEt.setText(extras.getString("cap"));
codeEt.setText(extras.getString("code"));
timeEt.setCurrentHour(extras.getInt("time"));
}
Button saveButton =(Button) findViewById(R.id.saveBtn);
saveButton.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
if (nameEt.getText().length() != 0)
{
AsyncTask<Object, Object, Object> saveContactTask =
new AsyncTask<Object, Object, Object>()
{
#Override
protected Object doInBackground(Object... params)
{
saveContact();
return null;
}
#Override
protected void onPostExecute(Object result)
{
finish();
}
};
saveContactTask.execute((Object[]) null);
}
else
{
AlertDialog.Builder alert = new AlertDialog.Builder(AddEditCountry.this);
alert.setTitle(R.string.errorTitle);
alert.setMessage(R.string.errorMessage);
alert.setPositiveButton(R.string.errorButton, null);
alert.show();
}
}
});
}
private void saveContact()
{
DatabaseConnector dbConnector = new DatabaseConnector(this);
if (getIntent().getExtras() == null)
{
dbConnector.insertContact(nameEt.getText().toString(),
capEt.getText().toString(),
timeEt.getCurrentHour().toString(),
codeEt.getText().toString());
}
else
{
dbConnector.updateContact(rowID,
nameEt.getText().toString(),
capEt.getText().toString(),
timeEt.getCurrentHour().toString(),
codeEt.getText().toString());
}
}
}
add_country xml (where TimePicker data which is causing the crash is first entered into the system)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1">
<LinearLayout android:id="#+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<EditText android:id="#+id/nameEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:hint="#string/name_hint"
android:inputType="textPersonName|textCapWords"/>
<EditText android:id="#+id/capEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:hint="#string/cap_hint"
android:inputType="textPersonName|textCapWords"/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data Limit"
android:textColor="#ffffff"
android:textAppearance="?android:textAppearanceMedium" />
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="left"
android:textColor="#ffffff"
android:text="10MB" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="right"
android:textColor="#ffffff"
android:text="Unlimited Data" />
</LinearLayout>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bandwidth Limit"
android:textColor="#ffffff"
android:textAppearance="?android:textAppearanceMedium" />
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="left"
android:textColor="#ffffff"
android:text="10kbs" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:textColor="#ffffff"
android:gravity="right"
android:text="Unlimited Bandwidth" />
</LinearLayout>
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceSmall" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WiFi Time Limit"
android:textColor="#ffffff"
android:textAppearance="?android:textAppearanceMedium" />
<TimePicker
android:id="#+id/timeEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1.0" />
<EditText
android:id="#+id/codeEdit"
android:inputType="textUri"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:lines="1"
android:hint="#string/code_hint"
android:imeOptions="actionNext" />
<Button android:id="#+id/saveBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_gravity="center_horizontal"
android:text="#string/save_btn"/>
</LinearLayout>
</ScrollView>
(UPDATED) LOGCAT:
03-22 06:14:34.855: D/Activity(4860): Activity.onPause(), editTextTapSensorList size: 0
03-22 06:14:34.915: I/Adreno200-EGLSUB(4860): <ConfigWindowMatch:2165>: Format RGBA_8888.
03-22 06:14:34.915: D/memalloc(4860): ion: Mapped buffer base:0x5d4c3000 size:614400 offset:0 fd:60
03-22 06:14:34.965: D/memalloc(4860): ion: Mapped buffer base:0x5d8f7000 size:614400 offset:0 fd:64
03-22 06:14:34.985: D/memalloc(4860): ion: Unmapping buffer base:0x5ca41000 size:614400
03-22 06:14:34.985: D/memalloc(4860): ion: Unmapping buffer base:0x5dc05000 size:614400
03-22 06:14:34.985: D/memalloc(4860): ion: Unmapping buffer base:0x5dc9b000 size:614400
03-22 06:14:35.035: D/memalloc(4860): ion: Mapped buffer base:0x5ca41000 size:614400 offset:0 fd:56
03-22 06:14:37.748: D/Activity(4860): Activity.onPause(), editTextTapSensorList size: 0
03-22 06:14:37.828: I/Adreno200-EGLSUB(4860): <ConfigWindowMatch:2165>: Format RGBA_8888.
03-22 06:14:37.828: D/memalloc(4860): ion: Mapped buffer base:0x5e131000 size:614400 offset:0 fd:59
03-22 06:14:37.828: W/ResourceType(4860): No package identifier when getting value for resource number 0x00000000
03-22 06:14:37.828: W/dalvikvm(4860): threadid=1: thread exiting with uncaught exception (group=0x410889d8)
03-22 06:14:37.838: E/AndroidRuntime(4860): FATAL EXCEPTION: main
03-22 06:14:37.838: E/AndroidRuntime(4860): android.content.res.Resources$NotFoundException: String resource ID #0x0
03-22 06:14:37.838: E/AndroidRuntime(4860): at android.content.res.Resources.getText(Resources.java:247)
03-22 06:14:37.838: E/AndroidRuntime(4860): at android.widget.TextView.setText(TextView.java:3622)
03-22 06:14:37.838: E/AndroidRuntime(4860): at com.nfc.linkingmanager.ViewCountry$LoadContacts.onPostExecute(ViewCountry.java:74)
03-22 06:14:37.838: E/AndroidRuntime(4860): at com.nfc.linkingmanager.ViewCountry$LoadContacts.onPostExecute(ViewCountry.java:1)
03-22 06:14:37.838: E/AndroidRuntime(4860): at android.os.AsyncTask.finish(AsyncTask.java:602)
03-22 06:14:37.838: E/AndroidRuntime(4860): at android.os.AsyncTask.access$600(AsyncTask.java:156)
03-22 06:14:37.838: E/AndroidRuntime(4860): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
03-22 06:14:37.838: E/AndroidRuntime(4860): at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 06:14:37.838: E/AndroidRuntime(4860): at android.os.Looper.loop(Looper.java:137)
03-22 06:14:37.838: E/AndroidRuntime(4860): at android.app.ActivityThread.main(ActivityThread.java:4477)
03-22 06:14:37.838: E/AndroidRuntime(4860): at java.lang.reflect.Method.invokeNative(Native Method)
03-22 06:14:37.838: E/AndroidRuntime(4860): at java.lang.reflect.Method.invoke(Method.java:511)
03-22 06:14:37.838: E/AndroidRuntime(4860): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
03-22 06:14:37.838: E/AndroidRuntime(4860): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
03-22 06:14:37.838: E/AndroidRuntime(4860): at dalvik.system.NativeStart.main(Native Method)
UI element "#+id/timeEdit" defined in layout view_country.xml is a TextView. You are tring to cast it to android.widget.TimePicker in your activity ViewCountry.
There is another UI element with same id ("#+id/timeEdit") which indeed IS TimePicker but it's placed in layout for AddEditCountry activity.
Check your ViewCountry layout and code, probably element #+id/timeEdit shouldn't be a TextView, or you should cast it to TextView in your onCreate method.
There is an error occurring in an app of mine when I set an Adapter to a ListView. Upon removing the 60/61 line of code (mainListViewTip/Hour.setAdapter(tip/hourAdapter);), the application runs perfectly, except since the adapter is never set, no data appears in the ListView. The goal is for the values of the EditText fields to become new values in the String to be displayed in a ListView along with previous entries also.
The TipBookActivity code:
public class TipBookActivity extends Activity {
/** Called when the activity is first created. */
TextView textTip,textHour,textWage;
EditText editHour,editTip;
float wage;
int precision = 100;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textTip = (TextView) findViewById(R.id.tvTip);
textHour = (TextView) findViewById(R.id.tvHour);
textWage = (TextView) findViewById(R.id.tvWage);
editTip = (EditText) findViewById(R.id.etTip);
editHour = (EditText) findViewById(R.id.etHour);
}
public void myClickHandler (View v){
Button bSubmit = (Button) findViewById(R.id.bSubmit);
bSubmit.isClickable();
ListView mainListViewTip = (ListView) findViewById(R.id.mainListViewTip);
ListView mainListViewHour = (ListView) findViewById(R.id.mainListViewHour);
switch(v.getId()){
case R.id.bSubmit:
if(bSubmit.isPressed()){
wage = Float.parseFloat(editTip.getText().toString()) / Float.parseFloat(editHour.getText().toString());
String tip = String.format("$%.2f",wage);
textWage.setText(String.valueOf(tip) + " an hour");
textHour.setText(editHour.getText() + " Hour(s)");
textTip.setText("$" + editTip.getText());
String[] sTip = new String[] {editTip.getText().toString()};
String[] sHour = new String[] {editHour.getText().toString()};
ArrayAdapter<String> tipAdapter = new ArrayAdapter<String>(TipBookActivity.this,R.layout.main,R.id.rowTextView,sTip);
ArrayAdapter<String> hourAdapter = new ArrayAdapter<String>(TipBookActivity.this,R.layout.main,R.id.rowTextView,sHour);
mainListViewTip.setAdapter(tipAdapter);
mainListViewHour.setAdapter(hourAdapter);
Toast displayWage = Toast.makeText(this, "$" + editTip.getText() + " over " + editHour.getText() + " hour(s) for a wage of $" + wage + " an hour.", Toast.LENGTH_LONG);
displayWage.show();
}
}
}
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater mMain = getMenuInflater();
mMain.inflate(R.menu.main_menu,menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
ViewFlipper vf = (ViewFlipper) findViewById(R.id.vfMain);
switch (item.getItemId()){
case R.id.menuHistory:
vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_in_right));
vf.showNext();
return true;
case R.id.menuClear:
//set up next tutorials
Toast displayClear = Toast.makeText(this, "Clear History feature coming soon.", Toast.LENGTH_SHORT);
displayClear.show();
return true;
case R.id.menuSettings:
Toast displaySettings = Toast.makeText(this, "Settings Options coming soon.", Toast.LENGTH_SHORT);
displaySettings.show();
return true;
}
return false;
}
public void onBackPressed() {
finish();
}
}
The main xml file:
<?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"
android:id="#+id/llMain" >
<ViewFlipper
android:id="#+id/vfMain"
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rletbtv">
<EditText
android:id="#+id/etTip"
android:layout_height="wrap_content"
android:layout_width="140dp"
android:hint="Tips"
android:layout_margin="8dp"
android:inputType="numberDecimal"
android:layout_alignParentLeft="true"/>
<EditText
android:id="#+id/etHour"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="Hours"
android:layout_margin="8dp"
android:inputType="numberDecimal"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/etTip"/>
<Button
android:id="#+id/bSubmit"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_margin="8dp"
android:ems="10"
android:text="Submit"
android:textSize="22sp"
android:layout_below="#id/etTip"
android:onClick="myClickHandler"/>
<TextView
android:id="#+id/tvTip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Tips"
android:textSize="22sp"
android:layout_margin="8dp"
android:layout_below="#id/bSubmit"/>
<TextView
android:id="#+id/tvHour"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Hours"
android:textSize="22sp"
android:layout_margin="8dp"
android:layout_below="#id/bSubmit"
android:layout_toRightOf="#id/tvTip"/>
<TextView
android:id="#+id/tvWage"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Wage"
android:textSize="22sp"
android:layout_margin="8dp"
android:layout_below="#id/bSubmit"
android:layout_toRightOf="#id/tvHour"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="50"
android:id="#+id/mainListViewTip"/>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="50"
android:id="#+id/mainListViewHour"/>
</LinearLayout>
</ViewFlipper>
</LinearLayout>
The simplerow xml file:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" />
The logcat error report:
FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:353)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
at android.widget.AbsListView.obtainView(AbsListView.java:1315)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
at android.widget.ListView.onMeasure(ListView.java:1109)
at android.view.View.measure(View.java:8171)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
at android.view.View.measure(View.java:8171)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
at android.view.View.measure(View.java:8171)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
at android.view.View.measure(View.java:8171)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
at android.view.View.measure(View.java:8171)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
at android.view.View.measure(View.java:8171)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
at android.view.View.measure(View.java:8171)
at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
The problem is because the layout you are passing to the ArrayAdapter is R.layout.main. This is not the resource you want to pass; you want to pass the layout for each individual list item. The layout you pass must have a TextView with a certain ID in it, and obviously R.layout.main does not have this.
Try substituting R.layout.main in those two lines to be android.R.layout.simple_list_item_1, and removing your custom TextView ID. That should solve the error and allow your list to display properly.
Like so:
ArrayAdapter<String> tipAdapter = new ArrayAdapter<String>(TipBookActivity.this,android.R.layout.simple_list_item_1,sTip);
ArrayAdapter<String> hourAdapter = new ArrayAdapter<String>(TipBookActivity.this,android.R.layout.simple_list_item_1,sHour);
If you later want to use a custom layout for the list items, try this tutorial or this one.
yes eric is right change this two lines of arrayadapter as shown below if you have not make layout for row item of listview
ArrayAdapter<String> tipAdapter = new ArrayAdapter<String>(TipBookActivity.this,R.layout.main,R.id.rowTextView,sTip);
ArrayAdapter<String> hourAdapter = new ArrayAdapter<String>(TipBookActivity.this,R.layout.main,R.id.rowTextView,sHour);
by
ArrayAdapter<String> tipAdapter = new ArrayAdapter<String>(TipBookActivity.this,android.R.layout.simple_list_item_1,sTip);
ArrayAdapter<String> hourAdapter = new ArrayAdapter<String>(TipBookActivity.this,android.R.layout.simple_list_item_1,sHour);
I think adam want to use his own textview so that he can change the text size and color which is not possible with "android.R.layout.simple_list_item_1". Instead of that we can use adam's "simplerow xml" file. to get this we need to replace 2 lines of the code
ArrayAdapter<String> tipAdapter = new ArrayAdapter<String>(TipBookActivity.this,R.layout.main,R.id.rowTextView,sTip);
ArrayAdapter<String> hourAdapter = new ArrayAdapter<String>(TipBookActivity.this,R.layout.main,R.id.rowTextView,sHour);
to
ArrayAdapter<String> tipAdapter = new ArrayAdapter<String>(TipBookActivity.this,R.layout.samplerow,R.id.rowTextView,sTip);
ArrayAdapter<String> hourAdapter = new ArrayAdapter<String>(TipBookActivity.this,R.layout.samplerow,R.id.rowTextView,sHour);
Note: the change is "R.layout.main" to "R.layout.samplerow"