2016-09-27 98 views
0

該應用程序開放正常,第一個Fragment顯示正確。在滑動屏幕上,下一個fragment佈局出現在屏幕上。在那裏,點擊按鈕,應用程序崩潰。ListFragment中的按鈕 - 點擊時應用程序崩潰

,我得到的錯誤是:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference 

一些從錯誤堆棧頂部條目如下:

at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:401) 

at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369) 

at android.widget.AbsListView.obtainView(AbsListView.java:2346) 

at android.widget.ListView.makeAndAddView(ListView.java:1876) 

at android.widget.ListView.fillDown(ListView.java:702) 

at android.widget.ListView.fillFromTop(ListView.java:763) 

at android.widget.ListView.layoutChildren(ListView.java:1685) 

at android.widget.AbsListView.onLayout(AbsListView.java:2148) 

at android.view.View.layout(View.java:16636) 

at android.view.ViewGroup.layout(ViewGroup.java:5437) 

應用程序的main activity如下:

public class MainActivity extends FragmentActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ViewPager pager = (ViewPager) findViewById(R.id.viewPager); 
    pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager())); 
} 

private class MyPagerAdapter extends FragmentPagerAdapter { 

    public MyPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     switch(position) { 

      case 0: return FirstFragment.newInstance("FirstFragment, Instance 1"); 
      case 1: return SecondFragment.newInstance("SecondFragment, Instance 1"); 
      default: return FirstFragment.newInstance("FirstFragment, Default"); 
     } 
    } 

} 
} 

SecondFragment.java如下:

public class SecondFragment extends ListFragment { 

static public ArrayAdapter<String> adapter; 
static String[] values = new String[3]; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.second_frag, container, 
      false); 

    final Button btn = (Button) rootView.findViewById(R.id.readWebpage); 
    if (btn != null) { 
     btn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v){ 
       new HttpGetReq().execute("http://xxx.xxx.xxx.xxx"); 
       adapter = new ArrayAdapter<String>(getActivity(), 
         android.R.layout.simple_list_item_1, values); 
       setListAdapter(adapter); 
      } 
     }); 
    } 
    return rootView; 
} 

public static SecondFragment newInstance(String text) { 

    SecondFragment f = new SecondFragment(); 
    Bundle b = new Bundle(); 
    b.putString("msg", text); 
    f.setArguments(b); 

    return f; 
} 


} 

單擊按鈕,HttpGetReq.java工人類從服務器獲取數據並將其顯示到列表中。

public class HttpGetReq extends AsyncTask<String , Void ,String> { 
String server_response; 

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

    URL url; 
    HttpURLConnection urlConnection = null; 

    try { 
     url = new URL(strings[0]); 
     urlConnection = (HttpURLConnection) url.openConnection(); 

     int responseCode = urlConnection.getResponseCode(); 

     if(responseCode == HttpURLConnection.HTTP_OK){ 
      server_response = readStream(urlConnection.getInputStream()); 
     } 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

@Override 
protected void onPostExecute(String s) { 
    super.onPostExecute(s); 
    Log.i("TAG", "Assigning new value"); 
    SecondFragment.values[1] = server_response; 
} 

// Converting InputStream to String 
private String readStream(InputStream in) { 
    BufferedReader reader = null; 
    StringBuffer response = new StringBuffer(); 
    try { 
     reader = new BufferedReader(new InputStreamReader(in)); 
     String line = ""; 
     while ((line = reader.readLine()) != null) { 
      response.append(line); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     if (reader != null) { 
      try { 
       reader.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
    return response.toString(); 
} 
} 

second_Frag.xml如下:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:id="@+id/myView2">> 
    <Button 
     android:id="@+id/readWebpage" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:onClick="onClick" 
     android:text="Refresh" 
     android:layout_gravity="center_horizontal"> 
    </Button> 
    <ListView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@id/android:list" 
     android:background="@color/background_Color" 
     android:layout_width="fill_parent" 
     android:gravity="center" 
     android:layout_height="fill_parent"/> 
</LinearLayout> 
+1

請提供日誌 – Nakul

+0

有什麼錯誤,你得到 –

+0

您正在試圖將值設置爲靜態數組。這是不允許的。跟蹤你的錯誤,並找到根本原因,併發布它可以幫助一點。 –

回答

0
new HttpGetReq().execute("http://xxx.xxx.xxx.xxx"); 
adapter = new ArrayAdapter<String>(getActivity(), 
     android.R.layout.simple_list_item_1, values); 
setListAdapter(adapter); 

這裏,從HttpGetReq獲取數據之前你設定適配器。由於HttpGetReq是一個AsyncTask,因此它在調用execute之後立即執行該行,將您的值數組保留爲空。從HttpGetReq的onPostExecute獲取數據後,應該將數據設置在適配器中。

@Override 
protected void onPostExecute(String s) { 
    super.onPostExecute(s); 
    Log.i("TAG", "Assigning new value"); 
    SecondFragment.values[1] = server_response; 

    // Set your data here 

} 
相關問題