Android NullpointerException错误和应用程序停止
我将通知属性添加到我的android应用程序中,并在google play中进行更新。 我的手机(Nexus 4)工作正常。 但有些手机会出现这种错误Android NullpointerException错误和应用程序停止,android,json,Android,Json,我将通知属性添加到我的android应用程序中,并在google play中进行更新。 我的手机(Nexus 4)工作正常。 但有些手机会出现这种错误 java.lang.NullPointerException at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116) at org.json.JSONTokener.nextValue(JSONTokener.java:94) at org.json.JSONObject.&
java.lang.NullPointerException
at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
at org.json.JSONTokener.nextValue(JSONTokener.java:94)
at org.json.JSONObject.<init>(JSONObject.java:154)
at org.json.JSONObject.<init>(JSONObject.java:171)
at com.medyasef.dernek.tjod.GetJson.json_to_last_id(GetJson.java:66)
at com.medyasef.dernek.tjod.PostService.onHandleIntent(PostService.java:42)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:213)
at android.os.HandlerThread.run(HandlerThread.java:60)
java.lang.NullPointerException
位于org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
位于org.json.JSONTokener.nextValue(JSONTokener.java:94)
位于org.json.JSONObject(JSONObject.java:154)
位于org.json.JSONObject(JSONObject.java:171)
在com.medyasef.dernek.tjod.GetJson.json_到_last_id(GetJson.java:66)
在com.medyasef.dernek.tjod.PostService.onhandleint(PostService.java:42)上
位于android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
位于android.os.Handler.dispatchMessage(Handler.java:99)
位于android.os.Looper.loop(Looper.java:213)
运行(HandlerThread.java:60)
我不明白为什么
PostService.java
public class PostService extends IntentService {
private List<Categoryicerikler> get_last_id;
private final String SON_ID_NUMARASI = "son_id_numarasi";
private final String UYARI = "preferences_sonucu";
private Context mContext = this;
public PostService() {
super("PostService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.i(UYARI, "Service Başladı");
if(checkInternetConnection()){
InternetConnection internetcon = new InternetConnection(34);
String json_result = internetcon.get_json_data();
try {
get_last_id = GetJson.json_to_last_id(json_result);
} catch (JSONException e) {
e.printStackTrace();
}
/*
Telefona kaydedilen son id numarasını kontrol ediyoruz.
Eğer daha önce internete bir veri kaydedilmediyse boş veri döner.
*/
SharedPreferences preferences = getSharedPreferences("last_id_int", MODE_PRIVATE);
int last_shared_id = preferences.getInt(SON_ID_NUMARASI, 0);
if(last_shared_id == 0) {
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(SON_ID_NUMARASI,get_last_id.get(0).getCategory_post_ID());
editor.apply();
Log.i(UYARI,"Sonuc Bos Dondu Preferences kaydetti");
}
else if(last_shared_id != get_last_id.get(0).getCategory_post_ID()){
Log.i(UYARI,"Sonuçlar Eşit Değil Notification Gosterilecek");
notification_show(get_last_id.get(0).getCategory_posttitle(), get_last_id.get(0).getCategory_post_content());
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(SON_ID_NUMARASI, get_last_id.get(0).getCategory_post_ID());
editor.apply();
}
else {
Log.i(UYARI,"Sonuclar Eşit Hiç Birşey Yapılmadı.");
}
}
}
private boolean checkInternetConnection() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {
return true;
}
else {
Log.v("Internet", "Internet Connection Not Present");
return false;
} }
private void notification_show(String post_title,String post_content){
int requestID = (int) System.currentTimeMillis();
Intent reminder_intent = new Intent(mContext, SinglePage.class);
reminder_intent.putExtra(Categories.EXTRA_CODE,post_content);
PendingIntent intent_single = PendingIntent.getActivity(this,requestID,reminder_intent,0);
/*
Setticker bildirim ilk geldiğinde üstte gözükecek yazıdır.
SetContentTitle Bildirim penceresi aşağı kaydırıldığında gösterilecek başlıktır.
setContentText Bildirim penceresi aşağı kaydırıldığında gösterilecek başlığın altında ki içeriktir.
setSmallIcon Gösterilecek resimdir.
*/
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification noti = new Notification.Builder(this)
.setTicker("TJOD Yeni Duyuru")
.setContentTitle(post_title)
.setContentText("Duyuruyu Görmek İçin Tıklayınız.")
.setSmallIcon(R.drawable.ic_launcher)
.setShowWhen(false)
.setContentIntent(intent_single)
.build();
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
}
公共类PostService扩展了IntentService{
私有列表获取最后一个id;
私有最终字符串SON\u ID\u NUMARASI=“SON\u ID\u NUMARASI”;
私有最终字符串UYARI=“首选项\u sonucu”;
私有上下文mContext=此;
公共邮政服务(){
超级(“邮政服务”);
}
@凌驾
受保护的手部内容无效(意图){
Log.i(UYARI,服务业律师);
如果(检查InternetConnection()){
InternetConnection internetcon=新的InternetConnection(34);
字符串json_result=internetcon.get_json_data();
试一试{
get_last_id=GetJson.json_to_last_id(json_结果);
}捕获(JSONException e){
e、 printStackTrace();
}
/*
凯德迪伦电视台的儿子是努马拉斯·科特罗·埃迪约鲁兹(numarasınıkontrol ediyoruz)。
艾尔·达汉斯·比尔韦里·卡德迪尔梅迪莉斯·博维里·德纳(boşveri döner)。
*/
SharedReferences首选项=获取SharedReferences(“最后一个id”,模式为私有);
int last_shared_id=preferences.getInt(SON_id_NUMARASI,0);
if(上次共享的\u id==0){
SharedReferences.Editor=首选项.edit();
putInt(SON_ID_NUMARASI,get_last_ID.get(0).getCategory_post_ID());
editor.apply();
Log.i(UYARI,Sonuc Bos Dondu Preferences kaydetti);
}
else if(last_shared_id!=get_last_id.get(0).getCategory_post_id()){
Log.i(UYARI,“Sonuçlar Eşit Değil通知Gosterelecek”);
通知显示(get_last_id.get(0).getCategory_posttitle(),get_last_id.get(0).getCategory_post_content());
SharedReferences.Editor=首选项.edit();
putInt(SON_ID_NUMARASI,get_last_ID.get(0).getCategory_post_ID());
editor.apply();
}
否则{
Log.i(UYARI,“Sonuclar Eşit HişBirşey Yapılmadı”);
}
}
}
专用布尔值checkInternetConnection(){
ConnectivityManager cm=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_服务);
如果(cm.getActiveNetworkInfo()!=null
&&cm.getActiveNetworkInfo().isAvailable()
&&cm.getActiveNetworkInfo().isConnected()){
返回true;
}
否则{
Log.v(“互联网”,“不存在互联网连接”);
返回false;
} }
私有无效通知\u显示(字符串post\u标题、字符串post\u内容){
int requestID=(int)System.currentTimeMillis();
意向提醒\意向=新意向(mContext,SinglePage.class);
提醒\意图.putExtra(类别.额外\代码,帖子内容);
PendingEvent intent\u single=PendingEvent.getActivity(this,requestID,提醒\u intent,0);
/*
塞蒂克·比尔迪里姆(bildirim ilk geldiğindeüstte gözükecek yazıdır)。
SetContentTitle Bildirim penceresi aşağkaydırıldıında gösterilecek başlıktır。
内容文本Bildirim penceresi aşaıkaydırıldında gösterilecek başlıııkayd içeriktir。
setSmallIcon Gösterilecek resimdir。
*/
NotificationManager NotificationManager=(NotificationManager)getSystemService(通知服务);
Notification noti=新建Notification.Builder(此)
.setTicker(“TJOD Yeni Duyuru”)
.setContentTitle(帖子标题)
.setContentText(“Duyuruyu Görmekçin Tıklayınız.”)
.setSmallIcon(R.drawable.ic_启动器)
.setShowWhen(错误)
.setContentIntent(单一意图)
.build();
noti.flags |=Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0,noti);
}
}
json_to_last_id函数(在GetJson.java中)
public static List json_to_last_id(字符串数据)抛出JSONException{
content_list=new ArrayList();
/*
这是一个很好的例子。
*/
JSONObject jObj=新的JSONObject(数据);
JSONArray posts=jObj.getJSONArray(“posts”);
JSONObject post_id=posts.getJSONObject(0);
JSONObject title=posts.getJSONObject(0);
JSONObject内容=posts.getJSONObject(0);
内容列表。添加(新的类别)(
title.getString(“title”),
content.getString(“content”),
post_id.getInt(“id”)
));
返回内容列表;
}
JSONTokener.nextCleanInternal
将在json输入字符串为空时抛出。检查data
参数是否为空json\u to\u last\u id
您能指出NPE在哪一行吗?这似乎是这一行:JSONObject jObj=new JSONObject(data);你能发布导致错误的json吗?@scottelme 42。行get_last_id=GetJson.json_to_last_id(json_结果)@njzk2如何
public static List<Categoryicerikler> json_to_last_id(String data) throws JSONException {
content_list = new ArrayList<Categoryicerikler>();
/*
Gelen String veriyi çekiyoruz ve dizilere girmeye başlıyoruz.
*/
JSONObject jObj = new JSONObject(data);
JSONArray posts = jObj.getJSONArray("posts");
JSONObject post_id = posts.getJSONObject(0);
JSONObject title = posts.getJSONObject(0);
JSONObject content = posts.getJSONObject(0);
content_list.add(new Categoryicerikler(
title.getString("title"),
content.getString("content"),
post_id.getInt("ID")
));
return content_list;
}