我正在设计一个库存清单程序,它记录具有各种属性的项目。我有它的工作,然后继续读取服务器上的文本文件,现在每当我尝试编辑属性和/或想要添加一个新项目它崩溃,我不知道为什么。我相信它与构造函数有关,但我尝试的每件事都没有用。
下面的是存储项目的列表元素:
public class ItemListActivity extends Activity implements OnItemClickListener
{
private ArrayAdapter<Item> adapter;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
//retrieve list of students and set them in the adaptor
ArrayList<Item> itemList = ((ItemApplication)getApplication()).getItemList();
adapter = new ArrayAdapter<Item>(this, android.R.layout.simple_list_item_1, itemList);
//assign adaptor to ListView
listView = (ListView)findViewById(R.id.item_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.itemmenu, menu);
return true;
}
@Override
protected void onResume()
{
super.onResume();
adapter.notifyDataSetChanged();
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.enter_item_data:
Intent intent = new Intent(this, EnterItemActivity.class);
startActivity(intent);
return true;
case R.id.get_network:
System.err.println("Read started");
GetDataFromServerTask t = new GetDataFromServerTask();
t.execute();
return true;
case R.id.send_network:
System.err.println("Send to network");
// comment out send code for now so we can test get- in isolation
//SendDataToServerTask st = new SendDataToServerTask();
//st.execute();
return true;
case R.id.save:
saveData();
return true;
case R.id.exit:
saveData();
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id)
{
//start EnterStudentActivity with reference to list position
Intent intent = new Intent (this, EnterItemActivity.class);
intent.putExtra("position", position);
startActivity(intent);
}
private void saveData()
{
// TODO Auto-generated method stub
}
public void showItemList()
{
adapter.notifyDataSetChanged();
}
@SuppressWarnings("rawtypes")
private class GetDataFromServerTask extends AsyncTask<Void, Void, Void>
{
private Socket socket = null;
private PrintWriter out = null;
private Scanner in = null;
protected Void doInBackground(Void ... params)
{
ArrayList<Item> ItemList = ((ItemApplication)getApplication()).getItemList();
try
{
if (socket == null)
{
InetAddress ia = InetAddress.getByName("10.0.2.2");
socket = new Socket(ia, 4444);
System.err.println("Created Socket");
out = new PrintWriter(socket.getOutputStream(), true);
System.err.println("Output stream created");
in = new Scanner(new InputStreamReader(socket.getInputStream()));
}
out.println("GET_LIST");;
in.useDelimiter(",");
while (in.hasNext())
{
String plant = in.next().trim();
int day = Integer.parseInt(in.next().trim());
int month = Integer.parseInt(in.next().trim());
int year = Integer.parseInt(in.next().trim());
int stockId = Integer.parseInt(in.next().trim());
String nameOfItem = in.next().trim();
int hour = 0;
int minute = 0;
String inspectorId = "";
String stockUnits = "";
String condition = "";
String additional = "";
Item i = new Item(plant, day, month, year, stockId, nameOfItem, hour, minute, inspectorId, stockUnits, condition, additional, true);
ItemList.add(i);
}
in.close();
out.close();
socket.close();
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host: ");
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for " + "the connection to: ");
}
catch (NullPointerException e)
{
System.err.println("Null pointer caught");
System.err.println(e.toString());
}
return null;
}
protected void onPostExecute(Void param)
{
ItemListActivity.this.showItemList();
}
}
}
下一批代码是项目的类:
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;
}
}
这最后一段代码是输入新项目时输入项目属性
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(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");
}
}
}
服务器崩溃的日志cat:
12-02 14:56:07.903: E/AndroidRuntime(2638): FATAL EXCEPTION: main
12-02 14:56:07.903: E/AndroidRuntime(2638): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.assignmentfinal/com.example.assignmentfinal.EnterItemActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x100
12-02 14:56:07.903: E/AndroidRuntime(2638): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-02 14:56:07.903: E/AndroidRuntime(2638): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-02 14:56:07.903: E/AndroidRuntime(2638): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-02 14:56:07.903: E/AndroidRuntime(2638): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-02 14:56:07.903: E/AndroidRuntime(2638): at android.os.Handler.dispatchMessage(Handler.java:99)
12-02 14:56:07.903: E/AndroidRuntime(2638): at android.os.Looper.loop(Looper.java:137)
12-02 14:56:07.903: E/AndroidRuntime(2638): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-02 14:56:07.903: E/AndroidRuntime(2638): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 14:56:07.903: E/AndroidRuntime(2638): at java.lang.reflect.Method.invoke(Method.java:511)
12-02 14:56:07.903: E/AndroidRuntime(2638): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-02 14:56:07.903: E/AndroidRuntime(2638): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-02 14:56:07.903: E/AndroidRuntime(2638): at dalvik.system.NativeStart.main(Native Method)
12-02 14:56:07.903: E/AndroidRuntime(2638): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x100
12-02 14:56:07.903: E/AndroidRuntime(2638): at android.content.res.Resources.getText(Resources.java:230)
12-02 14:56:07.903: E/AndroidRuntime(2638): at android.widget.TextView.setText(TextView.java:3640)
12-02 14:56:07.903: E/AndroidRuntime(2638): at com.example.assignmentfinal.EnterItemActivity.onCreate(EnterItemActivity.java:103)
12-02 14:56:07.903: E/AndroidRuntime(2638): at android.app.Activity.performCreate(Activity.java:5104)
12-02 14:56:07.903: E/AndroidRuntime(2638): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-02 14:56:07.903:E / AndroidRuntime(2638):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 12-02 14:56:07.903:E / AndroidRuntime(2638):... 11更多
下面是添加全新项目时应用程序崩溃的logcat: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
答案 0 :(得分:1)
错误在EnterItemActivity中的onCreate中,并且是Resources $ NotFoundException。 在logcat中,您可以看到这是通过调用:
产生的android.widget.TextView.setText
在onCreate中,您可以在几个地方调用setText。正是这个问题出现了:
etS.setText(item.getStockId());
您的方法返回一个int,而不是String:
public int getStockId()
{
return stockId;
}
setText方法有两个重载。使用字符串值调用时,即要使用的文本。当使用int调用时,该整数表示资源ID,并且Android无法找到所提供的id值的资源。我怀疑你只想将stockId的值显示为字符串。所以你可以改变你的方法:
public String getStockId()
{
return String.valueOf(stockId);
}
...或保留原样并使用:
etS.setText(String.valueOf(item.getStockId()));