我有一些数字ArrayList
。当活动恢复时,我将列表中的值与 a 的内容进行比较TextView
,如果它们相等,我将数据(数字)从ArrayList
to设置为EditText
,我得到 NPE。
到目前为止,我正在尝试这段代码:
public void onResume(){
super.onResume();
for (MenuInputClass mic2 : results) {
TextView namamenu2=(TextView)findViewById(R.id.nama_menu2);
String comp = namamenu2.getText().toString();
if (mic2.getNama_menu().equals(comp)) {
String jml_exis=mic2.getJumlah_menu();
int jml_exis_toint = Integer.parseInt(jml_exis);
jumlahmenu.setText(String.valueOf(jml_exis_toint));
}
}
}
我的MenuInputClass
:
public class MenuInputClass {
private String nama_menu;
private String jumlah_menu;
public void setNama_menu(String nama_menu) {
this.nama_menu=nama_menu;
}
public String getNama_menu() {
return (nama_menu);
}
public void setJumlah_menu(String jumlah_menu) {
this.jumlah_menu=jumlah_menu;
}
public String getJumlah_menu() {
return (jumlah_menu);
}
}
日志输出:
11-12 21:42:56.618: E/AndroidRuntime(19586): FATAL EXCEPTION: main
11-12 21:42:56.618: E/AndroidRuntime(19586): java.lang.RuntimeException: Unable to resume activity {com.example.aplikasipemesananmenu/com.example.aplikasipemesananmenu.Appetizer_Activity}: java.lang.NullPointerException
11-12 21:42:56.618: E/AndroidRuntime(19586): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124)
11-12 21:42:56.618: E/AndroidRuntime(19586): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
11-12 21:42:56.618: E/AndroidRuntime(19586): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
11-12 21:42:56.618: E/AndroidRuntime(19586): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-12 21:42:56.618: E/AndroidRuntime(19586): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-12 21:42:56.618: E/AndroidRuntime(19586): at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 21:42:56.618: E/AndroidRuntime(19586): at android.os.Looper.loop(Looper.java:130)
11-12 21:42:56.618: E/AndroidRuntime(19586): at android.app.ActivityThread.main(ActivityThread.java:3693)
11-12 21:42:56.618: E/AndroidRuntime(19586): at java.lang.reflect.Method.invokeNative(Native Method)
11-12 21:42:56.618: E/AndroidRuntime(19586): at java.lang.reflect.Method.invoke(Method.java:507)
11-12 21:42:56.618: E/AndroidRuntime(19586): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
11-12 21:42:56.618: E/AndroidRuntime(19586): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
11-12 21:42:56.618: E/AndroidRuntime(19586): at dalvik.system.NativeStart.main(Native Method)
11-12 21:42:56.618: E/AndroidRuntime(19586): Caused by: java.lang.NullPointerException
11-12 21:42:56.618: E/AndroidRuntime(19586): at com.example.aplikasipemesananmenu.Appetizer_Activity.onResume(Appetizer_Activity.java:231)
11-12 21:42:56.618: E/AndroidRuntime(19586): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
11-12 21:42:56.618: E/AndroidRuntime(19586): at android.app.Activity.performResume(Activity.java:3858)
11-12 21:42:56.618: E/AndroidRuntime(19586): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2114)
11-12 21:42:56.618: E/AndroidRuntime(19586): ... 12 more
Appetizer_Activity
:
public class Appetizer_Activity extends Activity{
static final String URL = "http://nervousme.vacau.com/android/xml_menu.xml";
static final String KEY_SONG2 = "result"; // parent node
static final String KEY_ID2 = "id_menu";
static final String KEY_NAMA_MENU2 = "nama_menu";
static final String KEY_KETERANGAN_MENU2 = "keterangan_menu";
static final String KEY_HARGA_MENU2 = "harga_menu";
static final String KEY_BAHAN_MENU2 = "bahan";
static final String KEY_THUMB_URL2 = "image";
final Context context = this;
AdapterDetailMenu adapterdetailmenu;
Button submitorder;
public static ArrayList<MenuInputClass> results = new ArrayList<MenuInputClass>();
public static ArrayAdapter<MenuInputClass> lelel;
public static ArrayList<String> list_list = new ArrayList<String>();
public static ArrayAdapter<String> adapter_pemesananmenu;
public static ListView list_pemesanan;
public static EditText jumlahmenu;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_view);
ListView list_single_list_view=(ListView)findViewById(R.id.list_single);
list_pemesanan = (ListView)findViewById(R.id.list);
submitorder=(Button)findViewById(R.id.btnSubmitMenu);
final Button plus = (Button)findViewById(R.id.btnPlus);
final Button minus = (Button)findViewById(R.id.btnMin);
jumlahmenu = (EditText)findViewById(R.id.txtJumlahMenu);
final ArrayList<HashMap<String, String>> songsList2 = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_SONG2);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID2, parser.getValue(e, KEY_ID2));
map.put(KEY_NAMA_MENU2, parser.getValue(e, KEY_NAMA_MENU2));
map.put(KEY_KETERANGAN_MENU2, parser.getValue(e, KEY_KETERANGAN_MENU2));
map.put(KEY_HARGA_MENU2, parser.getValue(e, KEY_HARGA_MENU2));
map.put(KEY_BAHAN_MENU2, parser.getValue(e, KEY_BAHAN_MENU2));
map.put(KEY_THUMB_URL2, parser.getValue(e, KEY_THUMB_URL2));
songsList2.add(map);
}
adapterdetailmenu=new AdapterDetailMenu(this, songsList2);
list_single_list_view.setAdapter(adapterdetailmenu);
adapter_pemesananmenu = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, list_list);
submitorder.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MenuInputClass sr = new MenuInputClass();
TextView namamenu=(TextView)findViewById(R.id.nama_menu2);
String hehe = namamenu.getText().toString();
String hihi = jumlahmenu.getText().toString();
sr.setNama_menu(hehe);
sr.setJumlah_menu(hihi);
results.add(sr);
ListPemesanan_Activity.list_pemesanan.setAdapter(new AdapterListPemesananMenu(getApplicationContext(), results));
Log.v("MYTAG", "results" + results);
Toast.makeText(Appetizer_Activity.this, "Berhasil di Tambahkan", Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onResume(){
super.onResume();
TextView namamenu2=(TextView)findViewById(R.id.nama_menu2);
String comp = namamenu2.getText().toString();
for(MenuInputClass mic2 : results) {
//int asd=results.indexOf(comp);
if(mic2.getNama_menu().equals(comp))
{
String jml_exis=mic2.getJumlah_menu();
int jml_exis_toint = Integer.parseInt(jml_exis);
jumlahmenu.setText(String.valueOf(jml_exis_toint));
}
}
}
}
布局截图了解
模拟器截图