Java 框架/页面适配器中的ListView为空
我正在创建一个基本的android应用程序,并使用创建的默认值创建第一个活动。此活动具有页面适配器。我希望在每个页面中都有不同的布局,并且希望在第一个页面中有一个Java 框架/页面适配器中的ListView为空,java,android,listview,Java,Android,Listview,我正在创建一个基本的android应用程序,并使用创建的默认值创建第一个活动。此活动具有页面适配器。我希望在每个页面中都有不同的布局,并且希望在第一个页面中有一个ListView,因此我做了以下操作: public static class DummySectionFragment extends Fragment { /** * The fragment argument representing the section number for this
ListView
,因此我做了以下操作:
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int page = getArguments().getInt(ARG_SECTION_NUMBER);
switch(page){
case 1:
SettingListAdapter settingAdapter = new SettingListAdapter(getActivity().getApplicationContext(), R.layout.setting_item_row, MainFeedActivity.mProfileList);
ListView listView = (ListView)this.getActivity().findViewById(R.id.listView1);
listView.setAdapter(settingAdapter);
break;
case 2:
break;
case 3:
break;
}
View rootView = inflater.inflate(R.layout.fragment_main_feed_dummy,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return rootView;
}
}
但是我的ListView
返回了null
,我不知道为什么。有谁能帮我弄清楚我的代码哪里做错了吗
如果您需要,以下是整个课程:
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import com.facebook.RequestBatch;
import com.redclay.fanaddict.R;
import com.redclay.fanaddict.helpers.Parser;
import com.redclay.fanaddict.helpers.SettingListAdapter;
import com.redclay.fanaddict.models.DeviceSettings;
import com.redclay.fanaddict.models.UserProfile;
public class MainFeedActivity extends FragmentActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
private String jsonResponse = null;
private static DeviceSettings mDeviceSettings;
private static List<UserProfile> mProfileList = new ArrayList<UserProfile>();
public static List<Long> mProfileIdList = new ArrayList<Long>();
private final String URL = "http://fanaddictsweb.redcley.com/Services/UserProfileService.svc/2/login";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_feed);
RequestUserFeed feed = new RequestUserFeed();
feed.execute(URL);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_feed, menu);
return true;
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_main_feed).toUpperCase(l);
case 1:
return getString(R.string.title_settings).toUpperCase(l);
case 2:
return getString(R.string.title_about).toUpperCase(l);
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int page = getArguments().getInt(ARG_SECTION_NUMBER);
switch(page){
case 1:
SettingListAdapter settingAdapter = new SettingListAdapter(getActivity().getApplicationContext(), R.layout.setting_item_row, MainFeedActivity.mProfileList);
ListView listView = (ListView)this.getActivity().findViewById(R.id.listView1);
listView.setAdapter(settingAdapter);
break;
case 2:
break;
case 3:
break;
}
View rootView = inflater.inflate(R.layout.fragment_main_feed_dummy,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return rootView;
}
}
class RequestUserFeed extends AsyncTask<String, String, HttpResponse>{
HttpResponse response = null;
@Override
protected HttpResponse doInBackground(String... uri) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(uri[0]);
post.addHeader("Content-Type", "application/json");
try {
JSONObject json = new JSONObject();
json.put("UserName", "michigan");
json.put("Password", "fanaddicts");
json.put("DeviceHardwareId", "NW58xfxz/w+jCiI3E592degUCL4=");
json.put("DeviceTypeId", "1");
StringEntity se = new StringEntity(json.toString());
Log.i("Feed Request", "SE: " + json.toString());
post.setEntity(se);
response = httpClient.execute(post);
Log.i("Feed Response", "Feed: " + response.getStatusLine().getStatusCode());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(HttpResponse result) {
HttpEntity entity = result.getEntity();
try {
jsonResponse = EntityUtils.toString(entity);
System.out.println("Resposne: " + jsonResponse );
Parser parser = new Parser(jsonResponse);
MainFeedActivity.mDeviceSettings = parser.getmDeviceSettings();
MainFeedActivity.mProfileList = parser.getmProfileList();
MainFeedActivity.mProfileIdList = parser.getUserProfileIds(MainFeedActivity.mProfileList);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class RequestSocialMediaFeeds extends AsyncTask<List<Long>, String, String>{
@Override
protected String doInBackground(List<Long>... arg0) {
List<Long> mList = arg0[0];
RequestBatch batch = new RequestBatch();
return null;
}
}
}
import java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Locale;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.ParseException;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.entity.StringEntity;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.util.EntityUtils;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.support.v4.app.FragmentActivity;
导入android.support.v4.app.FragmentManager;
导入android.support.v4.app.FragmentPagerAdapter;
导入android.support.v4.view.ViewPager;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.Menu;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ListView;
导入android.widget.TextView;
导入com.facebook.RequestBatch;
进口com.redclay.fandactive.R;
导入com.redclay.fanaddice.helpers.Parser;
导入com.redclay.fanactinc.helpers.SettingListAdapter;
导入com.redclay.fanaddict.models.DeviceSettings;
导入com.redclay.fanaddice.models.UserProfile;
公共类MainFeedActivity扩展了FragmentActivity{
/**
*将提供的{@link android.support.v4.view.PagerAdapter}
*每个部分的片段。我们使用
*{@link android.support.v4.app.FragmentPagerAdapter}派生,其中
*将所有加载的片段保留在内存中。如果这变得太内存化
*密集型,最好换成
*{@link android.support.v4.app.FragmentStatePagerAdapter}。
*/
分段SPAGERADAPTER mSectionsPagerAdapter;
/**
*将承载节内容的{@link ViewPager}。
*/
ViewPager mViewPager;
私有字符串jsonResponse=null;
专用静态设备设置mDeviceSettings;
私有静态列表mProfileList=newarraylist();
public static List mProfileIdList=new ArrayList();
私有最终字符串URL=”http://fanaddictsweb.redcley.com/Services/UserProfileService.svc/2/login";
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u main\u feed);
RequestUserFeed=新的RequestUserFeed();
feed.execute(URL);
//创建适配器,该适配器将为这三个函数中的每一个返回一个片段
//应用程序的主要部分。
mSectionsPagerAdapter=新节spageradapter(
getSupportFragmentManager());
//使用分区适配器设置ViewPager。
mViewPager=(ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main_feed,menu);
返回true;
}
/**
*一个{@link FragmentPagerAdapter},返回对应于
*其中一个部分/选项卡/页面。
*/
公共类节SpagerAdapter扩展了FragmentPagerAdapter{
公共部分SpagerAdapter(碎片管理器fm){
超级(fm);
}
@凌驾
公共片段getItem(int位置){
//调用getItem来实例化给定页面的片段。
//返回DummySectionFragment(定义为静态内部类
//下面)将页码作为其唯一参数。
Fragment Fragment=新的DummySectionFragment();
Bundle args=新Bundle();
args.putInt(DummySectionFragment.ARG_节号,位置+1);
fragment.setArguments(args);
返回片段;
}
@凌驾
public int getCount(){
//显示共3页。
返回3;
}
@凌驾
公共字符序列getPageTitle(int位置){
Locale l=Locale.getDefault();
开关(位置){
案例0:
返回getString(R.string.title\u main\u feed).toUpperCase(l);
案例1:
返回getString(R.string.title\u设置).toUpperCase(l);
案例2:
返回getString(R.string.title_about).toUpperCase(l);
}
返回null;
}
}
/**
*一个虚拟片段,表示应用程序的一部分,但是
*显示虚拟文本。
*/
公共静态类DummySectionFragment扩展了片段{
/**
*表示此文件节号的片段参数
*碎片。
*/
公共静态最终字符串ARG\u SECTION\u NUMBER=“SECTION\u NUMBER”;
公共数据段(){
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
int page=getArguments().getInt(参数节号);
交换机(第页){
案例1:
SettingListAdapter settingAdapter=新的SettingListAdapter(getActivity().getApplicationContext(),R.layout.setting_item_行,MainFeedActivity.mProfileList);
ListView ListView=(ListView)this.g
View rootView = inflater.inflate(R.layout.fragment_main_feed_dummy,
container, false);
ListView listView = (ListView)this.getActivity().findViewById(R.id.listView1);