2016-12-24 91 views
0

我想動態地添加項目到列表視圖。但是,我得到一個運行時異常。請幫助。全局變量「值」有問題嗎?我試圖通過打印日誌進行調試,並且能夠看到存儲在變量中的值。當它返回結果(我使用它,因爲我從數據庫獲取信息)時發生問題android從數據庫中獲取後返回null無法啓動活動ComponentInfo:java.lang.NullPointerException:storage == null

它從數據庫中獲取良好,但是當它返回結果時,嘗試塊 int在函數結束時跳轉到onther返回語句並且然後它崩潰

代碼

public class choose_buffet_items extends AppCompatActivity { 
 
    String []categouries; 
 
    ArrayAdapter adapter; 
 
    private static int SPLASH_TIME_OUT=2000; 
 
    AlertDialog alertDialog; 
 
    private boolean loggedIn = false; 
 
    String type; 
 
    String result; 
 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_choose_buffet_items); 
 
     new JSONParse().execute(); 
 
     adapter = new ArrayAdapter<String>(this, 
 
       R.layout.buffet_list_view, categouries); 
 

 
     ListView listView = (ListView) findViewById(R.id.listView_buffetitem); 
 
     listView.setAdapter(adapter); 
 
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
 
      @Override 
 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
 
       String s =(String)adapter.getItem(i); 
 
       if(s.equals("Drinks")) 
 
       { 
 
        Intent numbersIntent = new Intent(choose_buffet_items.this, buffet_drinks.class); 
 
        startActivity(numbersIntent); 
 
       } 
 
       else if(s.equals("Cakes")) 
 
       { 
 
        Intent numbersIntent = new Intent(choose_buffet_items.this, buffet_cake.class); 
 
        startActivity(numbersIntent); 
 
       } 
 
       else if(s.equals("Appetizers")) 
 
       { 
 
        Intent numbersIntent = new Intent(choose_buffet_items.this,buffet_appetizers.class); 
 
        startActivity(numbersIntent); 
 
       } 
 
       else {Intent numbersIntent = new Intent(choose_buffet_items.this, buffet_lunch.class); 
 
        startActivity(numbersIntent);} 
 

 
      } 
 
     }); 
 

 

 
     listView.setAdapter(adapter); 
 
    } 
 
    private class JSONParse extends AsyncTask<String, String, String> { 
 
     private ProgressDialog pDialog; 
 

 
     @Override 
 
     protected void onPreExecute() { 
 
      super.onPreExecute(); 
 
      alertDialog=new AlertDialog.Builder(choose_buffet_items.this).create(); 
 
      alertDialog.setTitle("Upadting Data"); 
 
      pDialog = new ProgressDialog(choose_buffet_items.this); 
 
      pDialog.setMessage("Getting Data ..."); 
 
      pDialog.setIndeterminate(false); 
 
      pDialog.setCancelable(true); 
 
      pDialog.show(); 
 

 

 
     } 
 

 

 
     @Override 
 
     protected String doInBackground(String... args) { 
 

 

 

 
       String login_URL = "http://2f179dfb.ngrok.io/getcat.php"; 
 
       try { 
 

 
        //Fetching the boolean value form sharedpreferences 
 

 

 
        URL url = new URL(login_URL); 
 
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
 
        httpURLConnection.setRequestMethod("POST"); 
 
        httpURLConnection.setDoOutput(true); 
 
        httpURLConnection.setDoInput(true); 
 

 
        InputStream inputStream = httpURLConnection.getInputStream(); 
 
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1")); 
 
        result = ""; 
 
        String line = ""; 
 
        while ((line = bufferedReader.readLine()) != null) { 
 
         result += line; 
 
        } 
 
        bufferedReader.close(); 
 
        inputStream.close(); 
 
        httpURLConnection.disconnect(); 
 
        Log.e("RESULT", result); 
 
        JSONObject jsonObject = new JSONObject(result); 
 

 
        JSONArray result1 = jsonObject.getJSONArray("result"); 
 
        categouries=new String[result1.length()]; 
 
        for (int i = 0; i < result1.length(); i++) { 
 
         JSONObject c = result1.getJSONObject(i); 
 
         String category = c.getString("buffCategory"); 
 
         categouries[i]=category; 
 
        } 
 

 
        return result; 
 

 

 
       } 
 
       catch (MalformedURLException e) { 
 
        e.printStackTrace(); 
 
       } catch (UnsupportedEncodingException e) { 
 
        e.printStackTrace(); 
 
       } catch (ProtocolException e) { 
 
        e.printStackTrace(); 
 
       } catch (IOException e) { 
 
        e.printStackTrace(); 
 
       } catch (JSONException e) { 
 
        e.printStackTrace(); 
 
       } 
 
       return result; 
 
      } 
 

 

 

 

 
     @Override 
 
     protected void onPostExecute(String r) { 
 
      adapter.notifyDataSetChanged(); 
 
      pDialog.dismiss(); 
 

 

 

 
     } 
 
    } 
 
}
logcat的

12-25 02:45:27.532 7109-7109/com.rematchka.weddinghall E/AndroidRuntime: FATAL EXCEPTION: main 
 
                     Process: com.rematchka.weddinghall, PID: 7109 
 
                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rematchka.weddinghall/com.rematchka.weddinghall.choose_buffet_items}: java.lang.NullPointerException: storage == null 
 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
 
                      at android.os.Looper.loop(Looper.java:148) 
 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
 
                      at java.lang.reflect.Method.invoke(Native Method) 
 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
 
                      Caused by: java.lang.NullPointerException: storage == null 
 
                      at java.util.Arrays$ArrayList.<init>(Arrays.java:38) 
 
                      at java.util.Arrays.asList(Arrays.java:155) 
 
                      at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:137) 
 
                      at com.rematchka.weddinghall.choose_buffet_items.onCreate(choose_buffet_items.java:49) 
 
                      at android.app.Activity.performCreate(Activity.java:6237) 
 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java)  
 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
 
                      at android.os.Handler.dispatchMessage(Handler.java:102)  
 
                      at android.os.Looper.loop(Looper.java:148)  
 
                      at android.app.ActivityThread.main(ActivityThread.java:5417)  
 
                      at java.lang.reflect.Method.invoke(Native Method)  
 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  
 
12-25 02:45:27.962 7109-7473/com.rematchka.weddinghall E/RESULT: {"result":[{"buffCategory":"Drinks"},{"buffCategory":"Cakes"},{"buffCategory":"Appetizers"},{"buffCategory":"Lunch"}]}

+2

添加logcat的,這樣的人能看到飛機墜毀 – yanivtwin

+0

它崩潰,當它在doInBackground功能 –

+0

你想在後臺做什麼返回,因爲你的結果變量是持有Json字符串,如果它直接傳遞給onPostExecute(),那麼爲什麼你在doInBackground中解析JSON –

回答

1

此特定的異常( 「存儲== NULL」)當傳遞給ArrayAdapter數組參數(上下文,INT,T [])爲空,則拋出。

在此行中onCreate方法:

adapter = new ArrayAdapter<String>(this, 
      R.layout.buffet_list_view, categouries); 

Categouries爲空,你需要對它進行初始化,然後才做這一行。

1

你可以使用List<String>而不是String[],你知道嗎?

無論如何,你假設一個AsyncTask立即完成。它不是。

將數組放入適配器時,您的數組爲null。

相反,調用的AsyncTask,但直到它準備

public class choose_buffet_items extends AppCompatActivity { 
    String []categouries; 
    ArrayAdapter adapter; 
    ListView listView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_choose_buffet_items); 
     new JSONParse().execute(); // Let this work in the background... 

     // adapter = new ArrayAdapter<String>(this, 
     //  R.layout.buffet_list_view, categouries); 

設置適配器不使用數據時,該名單是不是在onPostExecute

@Override 
    protected void onPostExecute(String r) { 
     adapter = new ArrayAdapter<String>(choose_buffet_items.this, 
      R.layout.buffet_list_view, categouries); 
     listView.setAdapter(adapter); 

     pDialog.dismiss(); 
    } 

注意空:如果您使您的AsyncTask使用<Void, Void, String[]>,(Void因爲您沒有任何參數或進度進行監控),那麼您可以將return的字符串排列爲return。現在,你回到原始JSON字符串,但從來沒有使用它onPostExecute

相關問題