无法恢复活动Java lang * NPE

时间:2012-11-12 14:46:17

标签: android android-activity

我在ArrayList中有一些数字。恢复活动后,我会将列表中的值与TextView的内容进行比较,如果它们相等,我会将数据(数字)从ArrayList设置为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);
    }
}

Logcat输出:

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));
            }                   
        }
    }
}

布局截图了解

enter image description here

模拟器截图

enter image description here

1 个答案:

答案 0 :(得分:0)

您可能需要阅读有关AdapterViews的文档部分。一般来说,您无法通过其ID在AdapterView中检索视图。此外,您不能绝对以这种方式访问​​其他活动的字段(即使用像MyActivity.widget这样的静态字段。)

使用ListView的常用模式是:

  1. 在XML布局中声明ListView并为其指定ID
  2. 为行创建布局
  3. 按ID检索ListView并设置其适配器(通常位于onCreate()
  4. 当您需要在活动之间进行通信时,请使用Intent框架(guide)。在某些情况下,您可能需要覆盖onNewIntent()