我在Android
Appiclation中创建CustomListAdapter。但是我NullPointerException
在执行它时得到这个错误。
这是我的CustomListAdapter.java代码:
/**
*
*/
package com.fanjavaid.searchhttprequest.adapter;
import java.util.List;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.fanjavaid.searchhttprequest.R;
import com.fanjavaid.searchhttprequest.app.AppController;
import com.fanjavaid.searchhttprequest.model.Menu;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
/**
* @author fanjavaid
*
*/
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Menu> menuItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomListAdapter(Activity activity, List<Menu> menuItems) {
this.activity = activity;
this.menuItems = menuItems;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return menuItems.size();
}
@Override
public Object getItem(int location) {
// TODO Auto-generated method stub
return menuItems.get(location);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView.findViewById(R.id.thumbnail);
TextView menuTitle = (TextView) convertView.findViewById(R.id.menuTitle);
TextView inputDate = (TextView) convertView.findViewById(R.id.postDate);
// Getting movie data for the row
Menu m = menuItems.get(position);
// thumbnail image
thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
// title
menuTitle.setText(m.getMenuTitle());
// input date
inputDate.setText("Post Date : " + String.valueOf(m.getInputDate().toString()));
return convertView;
}
}
这是我的活动代码:
package com.fanjavaid.searchhttprequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.fanjavaid.searchhttprequest.adapter.CustomListAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
public class ListMenuActivity extends Activity {
// Log tag
private static final String TAG = ListMenuActivity.class.getSimpleName();
// Movie JSON Url
private List<com.fanjavaid.searchhttprequest.model.Menu> menuList = new ArrayList<com.fanjavaid.searchhttprequest.model.Menu>();
private ListView listView;
private CustomListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_menu);
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, menuList);
listView.setAdapter(adapter);
Intent intent = getIntent();
//String jsonData = intent.getStringExtra("json_menu");
Bundle bundle = intent.getExtras();
String jsonString = bundle.getString("json_menu");
try {
JSONObject objectJson = new JSONObject(jsonString);
JSONArray arrayJson = objectJson.getJSONArray("results");
for (int i = 0; i < arrayJson.length(); i++) {
JSONObject obj = arrayJson.getJSONObject(i);
com.fanjavaid.searchhttprequest.model.Menu menu = new com.fanjavaid.searchhttprequest.model.Menu();
menu.setMenuTitle(obj.getString("nama_menu"));
menu.setThumbnailUrl("http://tempercube/receip_finder/" + obj.getString("gambar"));
String dateInString = obj.getString("tgl_input");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date parsedDate = formatter.parse(dateInString);
menu.setInputDate(parsedDate);
// add to list
menuList.add(menu);
}
// notifying list adapter about data changed
// so that it renders the list view with data changed
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
在我的活动中,已经在setAdapter()方法之前添加了CustomListAdapter实例化。但是我仍然得到那个NullPointerException
错误。
这是我的错误日志
09-18 09:11:34.661: E/AndroidRuntime(24097): FATAL EXCEPTION: main
09-18 09:11:34.661: E/AndroidRuntime(24097): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fanjavaid.searchhttprequest/com.fanjavaid.searchhttprequest.ListMenuActivity}: java.lang.NullPointerException
09-18 09:11:34.661: E/AndroidRuntime(24097): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
09-18 09:11:34.661: E/AndroidRuntime(24097): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2311)
09-18 09:11:34.661: E/AndroidRuntime(24097): at android.app.ActivityThread.access$600(ActivityThread.java:149)
09-18 09:11:34.661: E/AndroidRuntime(24097): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
09-18 09:11:34.661: E/AndroidRuntime(24097): at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 09:11:34.661: E/AndroidRuntime(24097): at android.os.Looper.loop(Looper.java:137)
09-18 09:11:34.661: E/AndroidRuntime(24097): at android.app.ActivityThread.main(ActivityThread.java:5214)
09-18 09:11:34.661: E/AndroidRuntime(24097): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 09:11:34.661: E/AndroidRuntime(24097): at java.lang.reflect.Method.invoke(Method.java:525)
09-18 09:11:34.661: E/AndroidRuntime(24097): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
09-18 09:11:34.661: E/AndroidRuntime(24097): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
09-18 09:11:34.661: E/AndroidRuntime(24097): at dalvik.system.NativeStart.main(Native Method)
09-18 09:11:34.661: E/AndroidRuntime(24097): Caused by: java.lang.NullPointerException
09-18 09:11:34.661: E/AndroidRuntime(24097): at com.fanjavaid.searchhttprequest.adapter.CustomListAdapter.<init>(CustomListAdapter.java:34)
09-18 09:11:34.661: E/AndroidRuntime(24097): at com.fanjavaid.searchhttprequest.ListMenuActivity.onCreate(ListMenuActivity.java:38)
09-18 09:11:34.661: E/AndroidRuntime(24097): at android.app.Activity.performCreate(Activity.java:5133)
09-18 09:11:34.661: E/AndroidRuntime(24097): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-18 09:11:34.661: E/AndroidRuntime(24097): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
09-18 09:11:34.661: E/AndroidRuntime(24097): ... 11 more
这是我的AppController.java
/**
*
*/
package com.fanjavaid.searchhttprequest.app;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.fanjavaid.searchhttprequest.util.LruBitmapCache;
import android.app.Application;
import android.text.TextUtils;
/**
* @author fanjavaid
*
*/
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public ImageLoader getImageLoader() {
getRequestQueue();
if (mImageLoader == null) {
mImageLoader = new ImageLoader(this.mRequestQueue,
new LruBitmapCache());
}
return this.mImageLoader;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
这是我的MainActivity.java
package com.fanjavaid.searchhttprequest;
import java.util.LinkedList;
import java.util.List;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView responseText;
private EditText searchText;
private String url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
responseText = (TextView) findViewById(R.id.textView1);
searchText = (EditText) findViewById(R.id.searchBox);
url = "http://tempercube.com/receip_finder/process_cari.php";
}
private String addLocationToUrl(String url, String parameter) {
if (!url.endsWith("?"))
url += "?";
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("bahan", parameter));
String paramString = URLEncodedUtils.format(params, "utf-8");
url += paramString;
return url;
}
public void doRequest(String passUrl) {
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
JsonObjectRequest jsonObjRequest = new JsonObjectRequest(Method.GET, passUrl, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// TODO Auto-generated method stub
Log.d("TAG", response.toString());
responseText.setText(response.toString());
Intent intent = new Intent("com.fanjavaid.searchhttprequest.ListMenuActivity");
Bundle extras = new Bundle();
extras.putString("json_menu", response.toString());
intent.putExtras(extras);
startActivity(intent);
pDialog.hide();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
Log.d("ERROR", "Error : " + error.getMessage());
pDialog.hide();
}
});
// Make request queue
VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjRequest);
}
public void searchMenu(View view) {
if(view.getId() == R.id.button1){
String paramSearch = searchText.getText().toString();
url = addLocationToUrl(url, paramSearch);
Log.d("URL_INFO", url);
doRequest(url);
// reset URL
url = "http://tempercube.com/receip_finder/process_cari.php";
}
}
@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, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
最后是我的list_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_row_selector">
</ListView>
</RelativeLayout>
请帮忙。非常感谢你。
我认为您没有将AppController添加到清单中:
<application
android:name=".AppController" //this one
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
这就是为什么onCreate从未调用且mInstance为null的原因。
问题内容: 当我尝试初始化eventBus时,出现NullPointerException异常: de.mrsfinster.web.livefeed.bean.FeedBean.init(FeedBean.java:179)处的java.lang.NullPointerException sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处sun
问题内容: 我正在尝试为在线Java课程创建程序。该程序包括Employee类和Name类。我必须创建多个Employee对象,并提示用户输入员工的姓名。我将所有Employee对象存储在一个employee数组中。 这是代码: 问题是编译器在运行程序时说最后一行是NullPointerException。我不确定自己在做什么错。有什么建议? 谢谢!-西恩 问题答案: 您创建了一个大小为的新数组,
问题内容: 我制作了一个250MB的json文件,看起来应该像这样: 其中“ B”值可以为len> =1。这表示我具有有效的JSON。 我打电话 这是文档。当读入熊猫数据框时,我得到以下回溯: 想不到出了什么问题。引发错误的python文件并没有帮助。 问题答案: 我有同样的错误消息,我使用绝对路径解决了。 那对我有用!
问题内容: 错误: 这是什么问题 问题答案: 某处正在修改。我怀疑它可能在此调用内发生: 编辑 基本上,发生的是通过 另一个 迭代器进行的递归修改。这使得外部迭代器的故障快速行为得以解决。 __
我正在使用Context.SendorderedBroadcast方法发送广播,并在到达ResultReceiver之前检查是否有人收到了广播。 下面是一段代码: 到目前为止,我只在搭载Android 2.1版本的摩托罗拉Defy手机上遇到过这种情况,但它也可能发生在其他手机上。 我在网上找不到任何关于它的参考资料,有人知道为什么会这样吗?谢谢!
问题内容: 我创建了一个自定义SSLSocketFactory类,并将其设置如下 从Eclipse Dev Environment运行并在命令提示符下将其作为Jar文件运行时,它可以正常工作。但是,当我将其包装在服务包装中并作为Windows Service启动时,它不起作用。我收到以下异常, 任何帮助??? 问题答案: 自从我发布此问题以来已经很长时间了。由于这篇文章没有任何答案,而且似乎也有一