2016-09-27 99 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 

} 
相关问题