I have some activity were user goes through it and finally send the selected data to the server to save in a db. the process is like this
activity1 user logs into the app and clicks OK
if sucessfull – activity2 the customer list is loaded in AutoCompleteTextView from the remote server.
The user selects the customer and goes to the next activity3
In acitivity3 user selects some data and send it to the server.Now when the sent is successfull I want the user to see activity2,which i am able to.But when i again select a customer from the autocomplete,i get NPEx. why??is the data erased when i move from one activity to the next.so in my case the data loaded in activity2 from the remote db is no more available i.e. the arraylist is null when i return to it.
i get the error on this line — for (Customer customer : customers)
@Override
public void onItemClick(AdapterView<?> adapter, View view, int pos, long rowId) {
String cusAddr = null;
int cusID = BaseKaizenActivity.getStorageManager().getCustomerId(customerTextView.getText().toString());
Log.d("CUSTOMER ID", Integer.toString(cusID));
for (Customer customer : customers) {
if (Integer.parseInt(customer.getId()) == cusID) {
cusAddr = customer.getAddress();
}
}
customerAddr.setTypeface(tf);
customerAddr.setText(Farsi.Convert(cusAddr));
//customerAddr.setText(cusAddr);
}
here is the log of the error
06-25 07:46:42.854: W/dalvikvm(572): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-25 07:46:42.864: E/AndroidRuntime(572): FATAL EXCEPTION: main
06-25 07:46:42.864: E/AndroidRuntime(572): java.lang.NullPointerException
06-25 07:46:42.864: E/AndroidRuntime(572): at com.pda.kaizen.activity.MainMenuActivity.onItemClick(MainMenuActivity.java:144)
06-25 07:46:42.864: E/AndroidRuntime(572): at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:952)
06-25 07:46:42.864: E/AndroidRuntime(572): at android.widget.AutoCompleteTextView.access$1400(AutoCompleteTextView.java:92)
06-25 07:46:42.864: E/AndroidRuntime(572): at android.widget.AutoCompleteTextView$DropDownItemClickListener.onItemClick(AutoCompleteTextView.java:1489)
06-25 07:46:42.864: E/AndroidRuntime(572): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
06-25 07:46:42.864: E/AndroidRuntime(572): at android.widget.ListView.performItemClick(ListView.java:3513)
06-25 07:46:42.864: E/AndroidRuntime(572): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
06-25 07:46:42.864: E/AndroidRuntime(572): at android.os.Handler.handleCallback(Handler.java:587)
06-25 07:46:42.864: E/AndroidRuntime(572): at android.os.Handler.dispatchMessage(Handler.java:92)
06-25 07:46:42.864: E/AndroidRuntime(572): at android.os.Looper.loop(Looper.java:123)
06-25 07:46:42.864: E/AndroidRuntime(572): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-25 07:46:42.864: E/AndroidRuntime(572): at java.lang.reflect.Method.invokeNative(Native Method)
06-25 07:46:42.864: E/AndroidRuntime(572): at java.lang.reflect.Method.invoke(Method.java:507)
06-25 07:46:42.864: E/AndroidRuntime(572): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-25 07:46:42.864: E/AndroidRuntime(572): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-25 07:46:42.864: E/AndroidRuntime(572): at dalvik.system.NativeStart.main(Native Method)
public class MainMenuActivity extends BaseKaizenActivity implements OnItemClickListener {
public AutoCompleteTextView customerTextView;
public EditText customerAddr;
private int customerId;
private List<Customer> customers;
private String customerName;
private Typeface tf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
Log.d("INSIDE MAINMENU ACTIVITY", "INSIDE MAINMENU ACTIVITY");
tf = Farsi.GetFarsiFont(this);
customerTextView = (AutoCompleteTextView) findViewById(R.id.cusName_CB);
customerTextView.setOnItemClickListener(this);
customerTextView.setThreshold(0);
customerTextView.setHint("Type Customer name");
customerAddr = (EditText) findViewById(R.id.editText_cusAddress);
// check loaded customers and products
// if haven't loaded - need to load
if (BaseKaizenActivity.getStorageManager().getCustomers().size() == 0) {
progressDialog = ProgressDialog.show(this, "Please Wait", "Loading customer list");
loadCustomers();
}
else {
handleSuccess(BaseKaizenActivity.getStorageManager().getCustomers());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.order_menu:
customerName = customerTextView.getText().toString();
if (!customerName.isEmpty() && (customerId = BaseKaizenActivity.getStorageManager().getCustomerId(customerName)) != 0) {
Log.d("----", " customer_id : " + customerId);
showOrderScreen(customerId);
}
else {
Toast.makeText(this, "Select a customer", Toast.LENGTH_LONG).show();
}
break;
case R.id.customer_menu:
//TODO - need implement
Toast.makeText(this, "You pressed the Customer!", Toast.LENGTH_LONG).show();
break;
}
return true;
}
private void loadCustomers() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try{
customers = getConnection().getCustomers();
BaseKaizenActivity.getStorageManager().setCustomers(customers);
Log.d("---", "loadedddddddd : customers: " + customers.size());
for(Customer c : customers)
{
Log.d("Customer name ", c.getName());
}
handleSuccess(customers);
}
catch (Exception exc) {
Log.d("--- ERROR ---", exc.getMessage());
handleException(exc.getMessage());
}
}
});
thread.start();
}
private void handleSuccess(final List<Customer> customers) {
runOnUiThread(new Runnable() {
@Override
public void run() {
final ArrayAdapter<Customer> customerSpinner = new ArrayAdapter<Customer>(MainMenuActivity.this,
android.R.layout.simple_spinner_item, customers);
customerSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
customerTextView.setAdapter(customerSpinner);
if (progressDialog != null) {
progressDialog.dismiss();
progressDialog = null;
}
}
});
}
@SuppressWarnings("unchecked")
private void showOrderScreen(int cusstomerId) {
showActivity(OrderActivity.class, new Pair<String, Integer>("CUSTOMER_ID", cusstomerId));
}
@Override
public void onItemClick(AdapterView<?> adapter, View view, int pos, long rowId) {
String cusAddr = null;
int cusID = BaseKaizenActivity.getStorageManager().getCustomerId(customerTextView.getText().toString());
Log.d("CUSTOMER ID", Integer.toString(cusID));
if(customers.isEmpty())
{
loadCustomers();
}
for (Customer customer : customers) {
if (Integer.parseInt(customer.getId()) == cusID) {
cusAddr = customer.getAddress();
}
}
customerAddr.setTypeface(tf);
customerAddr.setText(Farsi.Convert(cusAddr));
//customerAddr.setText(cusAddr);
}
}
I solved the issue and the problem was that when I would return the previous activity the arraylist was empty and did not contain the data which it had from the previous search. I fixed this issue by loading the data again in the OnCreate method.