我已经为ListView添加了一个“特色”页导航视图作为标题。pager视图从一个独立的url接收数据,而不是listview,所以我发出了两个独立的Volley请求。我已经在一个单独的项目中测试了pager视图,它完美无缺地工作。listview在没有额外的凌空请求的情况下工作得很好。这是我的主要活动。
public class MainActivity extends AppCompatActivity {
private ProgressDialog dialog=null ;
private CustomAdapter adapter;
private ListView list;
ArrayList<RowData> rowdata;
private SearchView searchView;
private MenuItem myActionMenuItem;
private ViewPager pageView;
private ArrayList<FeatureModel> fdata;
private FeatureAdapter featAdapt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup) inflater.inflate(R.layout.feature_view, list,
false);
pageView = (ViewPager)findViewById(R.id.featuredView);
list=(ListView) findViewById(R.id.listView1);
list.addHeaderView(header);
list.setTextFilterEnabled(true);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Get item at position
RowData item = (RowData)parent.getItemAtPosition(position);
//Pass the image title and url to DetailsActivity
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
intent.putExtra("title", item.getTitle());
intent.putExtra("image", item.getImageURL());
intent.putExtra("size", item.getProdSize());
intent.putExtra("date", item.getProdDate());
intent.putExtra("region", item.getRegionURL());
intent.putExtra("desc", item.getDetailURL());
//Start details activity
startActivity(intent);
}
});
rowdata=new ArrayList<RowData>();
dialog= new ProgressDialog(this, R.style.AppTheme);
dialog.setMessage("Loading...");
dialog.show();
JsonArrayRequest request = new JsonArrayRequest(url+url_file,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString()); try {
for(int i=0;i<response.length();i++){
String title=response.getJSONObject(i).getString("prod_name");
String size=response.getJSONObject(i).getString("prod_size");
String img_url;
String date = response.getJSONObject(i).getString("prod_date");
String detail_url = response.getJSONObject(i).getString("prod_desc");
String region_url = response.getJSONObject(i).getString("prod_region");
img_url = response.getJSONObject(i).getString("prod_image");
rowdata.add(new RowData(title, size, img_url,detail_url,region_url,date));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
adapter=new CustomAdapter(MainActivity.this, rowdata);
list.setAdapter(adapter);
dialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error: " + error.getMessage());
dialog.dismiss();
}
});
VolleyController.getInstance().addToRequestQueue(request, tag_json_arry);
loadFeatures();
}
public void loadFeatures(){
JsonArrayRequest stringRequest = new JsonArrayRequest(FEAT_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString()); try {
for(int i=0;i<response.length();i++){
String sid=response.getJSONObject(i).getString("fid");
String name=response.getJSONObject(i).getString("feat_name");
String img = response.getJSONObject(i).getString("feat_pic");
fdata.add(new FeatureModel(sid, name, img));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
featAdapt=new FeatureAdapter(MainActivity.this, fdata);
pageView.setAdapter(featAdapt);
//dialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error: " + error.getMessage());
//dialog.dismiss();
}
});
VolleyController.getInstance().addToRequestQueue(stringRequest, TAG);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
myActionMenuItem = menu.findItem(R.id.action_search);
searchView = (SearchView) myActionMenuItem.getActionView();
searchView.clearFocus();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
Filter filter = adapter.getFilter();
if (TextUtils.isEmpty(newText)) {
filter.filter("");
} else {
filter.filter(newText);
}
return true;
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
return true;
}
if(id == R.id.action_info){
return true;
}
if(id == R.id.action_share){
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
}
这是Logcat:
进程:com.curtrostudios.prodarchive,pid:7231 java.lang.nullpointerException at com.curtrostudios.prodarchive.mainActivity$4.onRespon(mainActivity.java:149)at com.curtrostudios.prodarchive.mainActivity$4.onRespon(mainActivity.java:149)at dispatchMessage(handler.java:95),android.os.looper.loop(looper.java:136),android.app.activitythread.main(activitythread.java:5072),java.lang.reflect.Method.invokenative(原生方法),java.lang.reflect.Method.invoke(Native
经过不断的研究和反复试验,我弄清楚了问题所在。问题不在于有两个Volley请求,而是Volley请求将JSON响应返回到空视图。
最初pagerview如下所示:
pageView = (ViewPager)findViewById(R.id.featuredView);
对于任何想要包含viewpager作为listview头的人来说,应该这样做。
View header = (RelativeLayout)getLayoutInflater().inflate(R.layout.feature_view, null);
pageView = (ViewPager)header.findViewById(R.id.featuredView);
然后,为了确保标题正确显示,我必须添加以下内容:
final DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
final int width = displayMetrics.widthPixels;
AbsListView.LayoutParams headerViewParams = new AbsListView.LayoutParams(width, 400);
header.setLayoutParams(headerViewParams);
这就是活动的onCreate()方法中的完整代码:
//Use this to find the width of the device
final DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
//This is where you declare the header layout
View header = (RelativeLayout)getLayoutInflater().inflate(R.layout.feature_view, null);
//This is where you find the view within the header layout
//that you're going to need or use
pageView = (ViewPager)header.findViewById(R.id.featuredView);
//This is where you set the layout params if the header layout
//isn't showing properly
AbsListView.LayoutParams headerViewParams = new AbsListView.LayoutParams(width, 400);
//Set the layout params for the header
header.setLayoutParams(headerViewParams);
list=(ListView)findViewById(R.id.listView1);
//add the header to the listview.
//in this case, ListView = list;
list.addHeaderView(header);
我正试着数字符串中元音的数目。问题是它编译正确,但当我运行它时,命令窗口显示“countvowels.main(countvowels.java:25)处线程'main'java.lang.NullPointerException”。 是什么导致了这一点,我该如何修复它,我该如何防止它在未来再次发生? 第25行是我的if语句:
我在活动onCreate()中初始化列表,如下所示: 编辑:我的活动的launchmode是SingleTask 编辑2: 我无法发送更多有用的日志,因为这次崩溃是在生产中。我只有一些布料日志。 谢谢你的帮助,但我不能粘贴整个代码,因为隐私。 我想我在SingleTask-OnCreate-OnNewIntent用法上有问题。简单地说,我试图打开我的应用程序从通知与参数决定哪个片段将打开时,用户导
我目前正在开发我的第一个游戏(我不会无耻地推广),但我正试图建立一个排行榜,但当我尝试初始化我的GameHelper时,我得到了一个例外。我找不到任何答案,如果有任何帮助,我将不胜感激。 代码: 我收到的错误是 “java.lang.RuntimeException:无法实例化活动组件信息{com.aidanstrong.runner/com.aidanstrong.runner.AndroidL
zapier中的Javascript代码 在Action类中,我正在获取请求数据 正在获取异常 java.lang.NullPointerException\n\tat java.io.StringReader
当我做POST请求调用使用JsonObjectRequest在凌空然后在某些wifi它是发送空的身体。但是它可以很好地处理StringRequest POST请求。它在所有移动网络上都运行良好。 我正在使用node。js服务器和expressjs/body解析器。当我使用邮递员发出邮件请求时,一切正常。 错误是什么?如果有人想看我能提供的代码。 使用StringRequest的POST请求 POS
我试图将谷歌地图添加到我的应用程序。logcat窗口显示此错误:由:java.lang.NullPointerExctive在com.example.mymaps.MainActivity.on创建(MainActivity.java:27)在android.app.Activity.perform创建(Activity.java:5104)在android.app.我nstrumentation