我有一些活动是用户通过它并最终将选定的数据发送到服务器以保存在数据库中。过程是这样的
活动 1 用户登录到应用程序,如果成功,则单击确定 - 活动 2 客户列表从远程服务器加载到 AutoCompleteTextView 中。用户选择客户并转到下一个活动 3 在活动 3 中,用户选择一些数据并将其发送到服务器。现在,当发送成功时,我希望用户看到活动 2,我能够看到。但是当我再次选择一个来自自动完成的客户,我得到了 NPEx。为什么??当我从一个活动移动到下一个活动时,数据会被删除。所以在我的情况下,从远程数据库加载到活动2 中的数据不再可用,即当我返回它时,arraylist 为空。
我在这条线上得到了错误---对于(客户客户:客户)
@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);
}
这是错误的日志
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)
公共类 MainMenuActivity 扩展 BaseKaizenActivity 实现 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);
}
}