ユーザーがそれを実行し、最後に選択したデータをサーバーに送信してデータベースに保存するというアクティビティがあります。プロセスはこのようなものです
activity1ユーザーはアプリにログインし、成功した場合は[OK]をクリックします-activity2顧客リストがリモートサーバーからAutoCompleteTextViewに読み込まれます。ユーザーは顧客を選択して次のアクティビティ3に進みます。actitivity3では、ユーザーはデータを選択してサーバーに送信します。送信が成功したら、ユーザーにアクティビティ2を表示してもらいたいのですが、もう一度選択するとオートコンプリートからの顧客、私はNPExを取得します。なぜ??あるアクティビティから次のアクティビティに移動するとデータが消去されるのですか?私の場合、リモートデータベースからactivity2にロードされたデータは使用できなくなります。つまり、配列リストに戻るとnullになります。
この行でエラーが発生します---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);
}
これがエラーのログです
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);
}
}