我有一个从文件中读取的库存系统,您应该也可以手动输入库存项目,程序从文件中读取确定但不会添加新项目,只要您单击添加按钮程序崩溃
下面的是日志猫:
12-02 15:07:03.171: E/AndroidRuntime(2803): FATAL EXCEPTION: main
12-02 15:07:03.171: E/AndroidRuntime(2803): java.lang.IllegalStateException: Could not execute method of the activity
12-02 15:07:03.171: E/AndroidRuntime(2803): at android.view.View$1.onClick(View.java:3597)
12-02 15:07:03.171: E/AndroidRuntime(2803): at android.view.View.performClick(View.java:4202)
12-02 15:07:03.171: E/AndroidRuntime(2803): at android.view.View$PerformClick.run(View.java:17340)
12-02 15:07:03.171: E/AndroidRuntime(2803): at android.os.Handler.handleCallback(Handler.java:725)
12-02 15:07:03.171: E/AndroidRuntime(2803): at android.os.Handler.dispatchMessage(Handler.java:92)
12-02 15:07:03.171: E/AndroidRuntime(2803): at android.os.Looper.loop(Looper.java:137)
12-02 15:07:03.171: E/AndroidRuntime(2803): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-02 15:07:03.171: E/AndroidRuntime(2803): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 15:07:03.171: E/AndroidRuntime(2803): at java.lang.reflect.Method.invoke(Method.java:511)
12-02 15:07:03.171: E/AndroidRuntime(2803): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-02 15:07:03.171: E/AndroidRuntime(2803): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-02 15:07:03.171: E/AndroidRuntime(2803): at dalvik.system.NativeStart.main(Native Method)
12-02 15:07:03.171: E/AndroidRuntime(2803): Caused by: java.lang.reflect.InvocationTargetException
12-02 15:07:03.171: E/AndroidRuntime(2803): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 15:07:03.171: E/AndroidRuntime(2803): at java.lang.reflect.Method.invoke(Method.java:511)
12-02 15:07:03.171: E/AndroidRuntime(2803): at android.view.View$1.onClick(View.java:3592)
12-02 15:07:03.171: E/AndroidRuntime(2803): ... 11 more
12-02 15:07:03.171: E/AndroidRuntime(2803): Caused by: java.lang.NullPointerException
12-02 15:07:03.171: E/AndroidRuntime(2803): at com.example.assignmentfinal.EnterItemActivity.onClick(EnterItemActivity.java:141)
12-02 15:07:03.171: E/AndroidRuntime(2803): ... 14 more
以下是项目类和输入项目类:
public class EnterItemActivity extends Activity implements OnEditorActionListener, OnItemSelectedListener, OnClickListener, android.content.DialogInterface.OnClickListener
{
private EditText et;
private EditText etP;
private EditText etS;
private EditText etN;
private EditText etSU;
private EditText etA;
private DatePicker dp;
private CheckBox cb;
private TextView tvSC;
private RadioButton rbE;
private RadioButton rbS;
private RadioButton rbU;
private Item item;
private TextView tv;
private TimePicker tp;
private Spinner spin;
String[] idItems = {"1253", "3965" , "3234", "9912", "3423"};
private boolean isNew = true;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_item);
etP = (EditText)this.findViewById(R.id.edit_plant);
etP.setOnEditorActionListener(this);
dp = (DatePicker)this.findViewById(R.id.dp_inspection_date);
etS= (EditText)this.findViewById(R.id.edit_stock_id);
etS.setOnEditorActionListener(this);
etN = (EditText)this.findViewById(R.id.edit_name_of_item);
etN.setOnEditorActionListener(this);
tp = (TimePicker)this.findViewById(R.id.tp_time_inspection);
tv = (TextView)this.findViewById(R.id.tv_inspector_id);
tv = (TextView)this.findViewById(R.id.tv_inspector_id_value);
spin = (Spinner)this.findViewById(R.id.sp_spinner_in_id);
spin.setOnItemSelectedListener(this);
ArrayAdapter inspectorSpinner = new ArrayAdapter(this, android.R.layout.simple_spinner_item, idItems);
inspectorSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(inspectorSpinner);
etSU = (EditText)this.findViewById(R.id.edit_stock_units);
etSU.setOnEditorActionListener(this);
rbE = (RadioButton)this.findViewById(R.id.rb_condition_excellent);
rbS = (RadioButton)this.findViewById(R.id.rb_condition_statisfactory);
rbU = (RadioButton)this.findViewById(R.id.rb_condition_unacceptable);
tvSC = (TextView)this.findViewById(R.id.tv_conditon_selected);
etA = (EditText)this.findViewById(R.id.edit_additional);
etA.setOnEditorActionListener(this);
cb = (CheckBox)this.findViewById(R.id.cb_follow_up);
//initialise the data if this is not a new student
//get the student object to be updated
Bundle extras = this.getIntent().getExtras();
if (extras != null)
{
isNew = false;
int position = extras.getInt("position");
item = ((ItemApplication)getApplication()).getItem(position);
etP.setText(item.getPlant());
etP.setFocusable(false); //can't update the name
dp.updateDate(item.getYear(), item.getMonth(), item.getDay());
tp.setCurrentHour(item.getHour());
tp.setCurrentMinute(item.getMinute());
etS.setText((String.valueOf(item.getStockId())));
etS.setFocusable(false);
etN.setText(item.getNameOfItem());
tv.setText(item.getInspectorId());
etSU.setText(item.getStockUnits());
tvSC.setText(item.getCondition());
etA.setText(item.getAdditional());
cb.setChecked(item.isFollowUp());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.itemmenu, menu);
return true;
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
InputMethodManager mgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(et.getWindowToken(), 0);
return true;
}
public void onClick(View v)
{
//get the values for displaying in the list
String plant = etP.getText().toString();
int day = dp.getDayOfMonth();
int month = dp.getMonth();
int year = dp.getYear();
int stockId = item.getStockId();
String nameOfItem = etN.getText().toString();
int hour = tp.getCurrentHour();
int minute = tp.getCurrentMinute();
String inspectorId = tv.getText().toString();
String stockUnits = etSU.getText().toString();
String condition = tvSC.getText().toString();
String additional = etA.getText().toString();
boolean isFollowUp= cb.isChecked();
if (isNew)
{
item = new Item(plant, day, month, year, stockId, nameOfItem, hour, minute, inspectorId, stockUnits, condition, additional, false);
((ItemApplication)getApplication()).addItem(item);
}
else
{
//update the student already in the list
item.setPlant(plant);
item.setDay(day);
item.setMonth(month);
item.setStockId(stockId);
item.setNameOfItem(nameOfItem);
item.setYear(year);
item.setHour(hour);
item.setMinute(minute);
item.setInspectorId(inspectorId);
item.setStockUnits(stockUnits);
item.setCondition(condition);
item.setAdditional(additional);
item.setFollowUp(isFollowUp);
}
showItem(item);
}
public void showItem(Item i)
{
new AlertDialog.Builder(this).setTitle("Item Details").setMessage(i.toString()).setNeutralButton("OK", this).show();
}
@Override
public void onClick(DialogInterface dialog, int which)
{
}
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id)
{
tv.setText(idItems [position]);
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
tv.setText("");
}
public void onRadioButtonClickCondition(View view)
{
if(rbE.isChecked())
{
tvSC.setText("Excellent");
}
else if(rbS.isChecked())
{
tvSC.setText("Satisfactory");
}
else if(rbU.isChecked())
{
tvSC.setText("Unacceptable");
}
}
}
public class Item
{
private String plant;
private int day, month, year;
private int hour, minute;
private int stockId;
private String nameOfItem;
private String inspectorId;
private String stockUnits;
private String condition;
private String additional;
private boolean isFollowUp;
public Item(String plant, int day, int month, int year, int stockId, String nameOfItem, int hour, int minute, String inspectorId, String stockUnits, String condition, String additional, boolean isFollowUp)
{
this.plant = plant;
this.day = day;
this.month = month;
this.year = year;
this.stockId = stockId;
this.nameOfItem = nameOfItem;
this.inspectorId = getInspectorId();
this.stockUnits = getStockUnits();
this.condition = getCondition();
this.additional = getAdditional();
this.isFollowUp = isFollowUp();
}
public String getPlant()
{
return plant;
}
public boolean isFollowUp()
{
return isFollowUp;
}
@Override
public String toString()
{
return "Plant: " + plant + "\n" + "Inspection Date: " + day + ", " + (month +1) + ", " + year +
"\n" + "Inspection Time: " + hour + ":" + minute + "\n" + "Stock ID: " + stockId + "\n" + "Name of Item: " + nameOfItem + "\n" + "Inspector ID: "
+ inspectorId + "\n" + "Stock Units: " + stockUnits + "\n" + "Condition: " + condition + "\n"
+ "Additional Info: " + additional +"\n" + "Follow-up? " + isFollowUp;
}
public Item(String plant, int day, int month, int year, int stockId, String nameOfItem, int hour, int minute, String inspectorId, String stockUnits, String condition, String additional)
{
this(plant, day, month, year, stockId, nameOfItem, hour, minute, inspectorId, stockUnits, condition, additional, true);
}
public int getDay()
{
return day;
}
public int getMonth()
{
return month;
}
public int getYear()
{
return year;
}
public void setPlant(String plant)
{
this.plant = plant;
}
public void setDay(int day)
{
this.day = day;
}
public void setMonth(int month)
{
this.month = month;
}
public void setYear(int year)
{
this.year = year;
}
public void setFollowUp(boolean isFollowUp)
{
this.isFollowUp = isFollowUp;
}
public int getStockId()
{
return stockId;
}
public void setStockId(int stockId)
{
this.stockId = stockId;
}
public String getNameOfItem()
{
return nameOfItem;
}
public void setNameOfItem(String nameOfItem)
{
this.nameOfItem = nameOfItem;
}
public String getInspectorId()
{
return inspectorId;
}
public void setInspectorId(String inspectorId)
{
this.inspectorId = inspectorId;
}
public String getStockUnits()
{
return stockUnits;
}
public void setStockUnits(String stockUnits)
{
this.stockUnits = stockUnits;
}
public String getCondition()
{
return condition;
}
public void setCondition(String condition)
{
this.condition = condition;
}
public String getAdditional()
{
return additional;
}
public void setAdditional(String additional)
{
this.additional = additional;
}
public int getHour()
{
return hour;
}
public void setHour(int hour)
{
this.hour = hour;
}
public int getMinute()
{
return minute;
}
public void setMinute(int minute)
{
this.minute = minute;
}
}