我正在JSON.I解析JSON,我可以顯示我的JSON列表視圖(圖像和文本),也成功創建了部件,我想在我的widget中顯示我的JSON的第一項標題。此代碼(如果我刪除小部件,然後我可以顯示小部件的第一個標題),但一段時間後,當我點擊小部件的按鈕,我有nullPointExcetpion 我做錯了什麼?Android部件nullpointexception
public class BriWidget extends AppWidgetProvider {
public static String CLOCK_WIDGET_UPDATE = "CLOCK_WIDGET_UPDATE";
RemoteViews remoteViews;
int appWidgetId;
public File file;
public Bitmap bitmap;
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
Toast.makeText(context, "TimeWidgetRemoved id(s):" + appWidgetIds,
Toast.LENGTH_SHORT).show();
super.onDeleted(context, appWidgetIds);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
ComponentName thisWidget = new ComponentName(context, BriWidget.class);
for (int widgetId : appWidgetManager.getAppWidgetIds(thisWidget)) {
Intent intent = new Intent(context, SpleshScreen.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);
remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_demo1);
remoteViews
.setOnClickPendingIntent(R.id.widgetbtn, pendingIntent);
remoteViews
.setOnClickPendingIntent(R.id.widgetImage, pendingIntent);
remoteViews.setOnClickPendingIntent(R.id.widgetdesc, pendingIntent);
remoteViews.setOnClickPendingIntent(R.id.widgetbtn,
buildButtonPendingIntent1(context));
remoteViews.setTextViewText(R.id.widgetdesc, getDesc());
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
@Override
public void onAppWidgetOptionsChanged(Context context,
AppWidgetManager appWidgetManager, int appWidgetId,
Bundle newOptions) {
Toast.makeText(context, "onAppWidgetOptionsChanged() called",
Toast.LENGTH_SHORT).show();
}
public static PendingIntent buildButtonPendingIntent1(Context context) {
Intent intent = new Intent();
intent.setAction(WidgetUtils.WIDGET_UPDATE_ACTION);
return PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
private static CharSequence getDesc() {
return "this is a widget example";
}
public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
ComponentName myWidget = new ComponentName(context, BriWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myWidget, remoteViews);
}
}
public class MyWidgetIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
wl.acquire();
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_demo1);
remoteViews.setTextViewText(R.id.widgetdesc, getDesc(""));
remoteViews.setOnClickPendingIntent(R.id.widgetbtn,
BriWidget.buildButtonPendingIntent1(context));
BriWidget
.pushWidgetUpdate(context.getApplicationContext(), remoteViews);
ComponentName thiswidget = new ComponentName(context, BriWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thiswidget, remoteViews);
wl.release();
}
public void setOnetimeTimer(Context context){
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MyWidgetIntentReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
}
private String getDesc(String abc) {
abc = (MainActivity.itemList.get(0).get(MainActivity.KEY_title))
.toString();
return abc;
}
}
公共類BRIgeAdapter延伸BaseAdapter {
Context mContext;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
private int screenSize;
public BRIgeAdapter(Context context, ArrayList<HashMap<String, String>> d,
int screenSize) {
this.mContext = context;
this.data = d;
this.screenSize = screenSize;
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(context.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.list_row, null);
TextView journal = (TextView) vi.findViewById(R.id.smalljournal);
TextView title = (TextView) vi.findViewById(R.id.smalltitle);
TextView description = (TextView) vi
.findViewById(R.id.smallDescription);
ImageView thumb_image = (ImageView) vi.findViewById(R.id.smallthumb);
TextView statId = (TextView) vi.findViewById(R.id.smallstatID);
TextView DateTime = (TextView) vi.findViewById(R.id.smallDateTime);
HashMap<String, String> itemList = new HashMap<String, String>();
itemList = data.get(position);
journal.setText(itemList.get(MainActivity.KEY_journal));
statId.setText(itemList.get(MainActivity.KEY_statID));
String titleString = itemList.get(MainActivity.KEY_title);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String DateTimeTxt = itemList.get(MainActivity.KEY_pubDate).replace(
"T", " ");
try {
Date _d = df.parse(DateTimeTxt);
SimpleDateFormat new_df = new SimpleDateFormat("dd.MM.yyyy");
String _s = new_df.format(_d);
DateTime.setText(_s);
} catch (ParseException e) {
e.printStackTrace();
}
if (screenSize == Configuration.SCREENLAYOUT_SIZE_NORMAL)
description.setVisibility(View.INVISIBLE);
else
description.setVisibility(View.VISIBLE);
title.setText(titleString);
description.setText(itemList.get(MainActivity.KEY_description));
String url = itemList.get(MainActivity.KEY_image);
imageLoader.DisplayImage(url, thumb_image);
return vi;
}
}
public class MainActivity extends Activity {
public String URL = "***********************";
public static String KEY_title = "title";
public static String KEY_description = "description";
public static String KEY_image = "image";
public static String KEY_journal = "journal";
public static String KEY_JournalID = "JournalID";
public static String KEY_pubDate = "pubDate";
public static String KEY_statID = "statID";
public JSONArray jsonarray;
public ListView list;
public JSONParser jsonparser;
static BRIgeAdapter adapter;
public static ArrayList<HashMap<String, String>> itemList;
public ImageLoader imageLoader;
static final int DIALOG_ERROR_CONNECTION = 1;
private ArrayList<Content> contents = new ArrayList<Content>();
public TransparentProgressDialog pd;
public HashMap<String, String> map;
public Tools tools;
private int screenSize;
public LoadDataAllChanelsToServer loadData;
private ConnectionDetector cd;
@SuppressLint("CutPasteId")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
screenSize = getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK;
itemList = new ArrayList<HashMap<String, String>>();
list = (ListView) findViewById(R.id.listView1);
cd = new ConnectionDetector(getApplicationContext());
adapter = new BRIgeAdapter(MainActivity.this, itemList, screenSize);
Thread.setDefaultUncaughtExceptionHandler(new UnCaughtException(this,
this));
loadData = new LoadDataAllChanelsToServer();
pd = new TransparentProgressDialog(this, R.drawable.loader);
loadData.execute();
}
private class LoadDataAllChanelsToServer extends
AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
pd.show();
}
@Override
protected String doInBackground(String... urls) {
jsonparser = new JSONParser();
JSONObject jsonobject = jsonparser.getJSONfromURL(URL);
try {
jsonarray = jsonobject.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
map = new HashMap<String, String>();
map.put("journal", jsonobject.getString(KEY_journal));
map.put("image", jsonobject.getString(KEY_image));
map.put("title", jsonobject.getString(KEY_title));
map.put("description",
jsonobject.getString(KEY_description));
map.put("JournalID", jsonobject.getString(KEY_JournalID));
map.put("pubDate", jsonobject.getString(KEY_pubDate));
map.put("statID", jsonobject.getString(KEY_statID));
Content cont = new Content(jsonobject.getString("journal"),
jsonobject.getString("image"),
jsonobject.getString("title"),
jsonobject.getString("pubDate"),
jsonobject.getString("description"),
jsonobject.getString("JournalID"),
jsonobject.getString("statID"));
contents.add(cont);
itemList.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return itemList.toString();
}
@Override
protected void onPostExecute(String result) {
try {
if (pd != null) {
pd.dismiss();
}
} catch (Exception e) {
}
try {
adapter = new BRIgeAdapter(MainActivity.this, itemList,
screenSize);
list.setAdapter(adapter);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
}
後的一段時間,我有nullpointexception在MyWidgetIntentReceiver類getDesc方法 ABC =(MainActivity.itemList.get(0)獲得(MainActivity.KEY_title)) 的ToString(); 是空
顯示你的logcat – rajshree
是的,我用logcat和nullpointexception是MyWidgetIntentReceiver getDesc方法 – user3293990
粘貼你的logcat像你發佈你的代碼的問題。 – InnocentKiller