【问题标题】:Working Android app got unstable正在运行的 Android 应用程序变得不稳定
【发布时间】:2015-04-30 02:10:06
【问题描述】:

我创建了一个 android 应用程序,该应用程序具有添加和编辑配置文件的功能。这些天一切都运行良好,但今天早上我突然在编辑配置文件后尝试保存更改时,它开始停止响应并退出。

我使用崩溃邮件捕获了错误,这是我得到的:

USER_COMMENT=null
ANDROID_VERSION=4.4.2
APP_VERSION_NAME=1.0
BRAND=samsung
PHONE_MODEL=SHV-E300L
CUSTOM_DATA=
STACK_TRACE=java.lang.NullPointerException: uriString
at android.net.Uri$StringUri.<init>(Uri.java:467)
at android.net.Uri$StringUri.<init>(Uri.java:457)
at android.net.Uri.parse(Uri.java:429)
at com.diwesh.Ecgapp.adapter.AdapterProfileList.getView(AdapterProfileList.java:78)
at android.widget.AbsListView.obtainView(AbsListView.java:2720)
at android.widget.ListView.makeAndAddView(ListView.java:1801)
at android.widget.ListView.fillDown(ListView.java:697)
at android.widget.ListView.fillFromTop(ListView.java:763)
at android.widget.ListView.layoutChildren(ListView.java:1627)
at android.widget.AbsListView.onLayout(AbsListView.java:2533)
at android.view.View.layout(View.java:15656)
at android.view.ViewGroup.layout(ViewGroup.java:4857)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
at android.view.View.layout(View.java:15656)
at android.view.ViewGroup.layout(ViewGroup.java:4857)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15656)
at android.view.ViewGroup.layout(ViewGroup.java:4857)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15656)
at android.view.ViewGroup.layout(ViewGroup.java:4857)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15656)
at android.view.ViewGroup.layout(ViewGroup.java:4857)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2288)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2008)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1238)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6473)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:573)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)

AdapterList.java

package com.diwesh.Ecgapp.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.diwesh.Ecgapp.R;
import com.diwesh.Ecgapp.db.ProfileDBManager.ProfileModel;
import com.diwesh.Ecgapp.doc.Globals;
import com.pkmmte.circularimageview.CircularImageView;


public class AdapterProfileList extends BaseAdapter {

    public List<ProfileModel> m_lstItems;    
    public int selItem = -1;
    public Context con;
//    private SwipeDetector swipeDetector;

    public AdapterProfileList(Context con)
    {
        super();
        this.con = con;
//        swipeDetector = new SwipeDetector();
    }
    public int getCount()
    {
        if (this.m_lstItems == null)
            return 0;
        return this.m_lstItems.size();
    }

    public ProfileModel getItem(int paramInt)
    {
        return this.m_lstItems.get(paramInt);
    }

    public long getItemId(int paramInt)
    {
        return 0L;
    }

    public View getView(int paramInt, View paramView, ViewGroup paramViewGroup)
    {
        View localView = paramView;
        ViewHolder localViewHolder = null;

        if (localView == null)
        {
            localView = LayoutInflater.from(paramViewGroup.getContext()).inflate(R.layout.item_profile, null);
        }
        else
        {
            localViewHolder = (ViewHolder) localView.getTag();
        }
        if (localViewHolder == null){
            localViewHolder = new ViewHolder();
            localViewHolder.m_item1 = ((CircularImageView)localView.findViewById(R.id.imgDoge));
            localViewHolder.m_item2 = ((TextView)localView.findViewById(R.id.txt_profile_item)); 
            localViewHolder.ll_swipe = ((LinearLayout)localView.findViewById(R.id.ll_profile));

            localView.setTag(localViewHolder);
        }

//        localViewHolder.ll_swipe.setOnTouchListener(swipeDetector);
        //add String
        final ProfileModel t = m_lstItems.get(paramInt);
        //localViewHolder.m_item1.setImageURI(Uri.parse(t.image));
        Bitmap bm = Globals.setImageScale(con, Uri.parse(t.image));
        localViewHolder.m_item1.setImageBitmap(bm);        
        String ss = t.firstName +  " " + t.lastName + " " + t.age +"years old";
        if (t.gender == 0)
        {
            ss = ss + " Male";
        }
        else
        {
            ss = ss + " Female";
        }
        localViewHolder.m_item2.setText(ss);    


        return localView;
    }

    public void update(List<ProfileModel> paramList)
    {
        this.m_lstItems = paramList;
        notifyDataSetChanged();
    }

    public class ViewHolder
    {
        public CircularImageView m_item1;
        public TextView m_item2;      
        public LinearLayout ll_swipe;
    }

    public void remove(int id) {
        // TODO Auto-generated method stub
        Log.e("LIST ITEMSL::", ""+m_lstItems);
        Log.e("mLIST ITEM SIZE::"+m_lstItems.size(), "ID>>>>"+id);
        m_lstItems.remove(m_lstItems.get(id));
        notifyDataSetChanged();
    }



}

请帮忙.....

编辑 1:按照建议添加了空检查:应用程序现在在重新启动时不会崩溃,但当我再次编辑后单击“配置文件”选项卡时仍然会崩溃。这是新的 logcat

USER_COMMENT=null
ANDROID_VERSION=4.4.2
APP_VERSION_NAME=1.0
BRAND=samsung
PHONE_MODEL=SHV-E300L
CUSTOM_DATA=
STACK_TRACE=java.lang.NullPointerException: uriString
at android.net.Uri$StringUri.<init>(Uri.java:467)
at android.net.Uri$StringUri.<init>(Uri.java:457)
at android.net.Uri.parse(Uri.java:429)
at com.diwesh.Ecgapp.ui.Fragment3.setData(Fragment3.java:149)
at com.diwesh.Ecgapp.ui.Fragment3.initControl(Fragment3.java:142)
at com.diwesh.Ecgapp.ui.Fragment3.onCreateView(Fragment3.java:75)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)

修改后的新AdapterList.java

package com.diwesh.Ecgapp.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.diwesh.Ecgapp.R;
import com.diwesh.Ecgapp.db.ProfileDBManager.ProfileModel;
import com.diwesh.Ecgapp.doc.Globals;
import com.pkmmte.circularimageview.CircularImageView;


public class AdapterProfileList extends BaseAdapter {

    public List<ProfileModel> m_lstItems;    
    public int selItem = -1;
    public Context con;
//    private SwipeDetector swipeDetector;

    public AdapterProfileList(Context con)
    {
        super();
        this.con = con;
//        swipeDetector = new SwipeDetector();
    }
    public int getCount()
    {
        if (this.m_lstItems == null)
            return 0;
        return this.m_lstItems.size();
    }

    public ProfileModel getItem(int paramInt)
    {
        return this.m_lstItems.get(paramInt);
    }

    public long getItemId(int paramInt)
    {
        return 0L;
    }

    public View getView(int paramInt, View paramView, ViewGroup paramViewGroup)
    {
        View localView = paramView;
        ViewHolder localViewHolder = null;

        if (localView == null)
        {
            localView = LayoutInflater.from(paramViewGroup.getContext()).inflate(R.layout.item_profile, null);
        }
        else
        {
            localViewHolder = (ViewHolder) localView.getTag();
        }
        if (localViewHolder == null){
            localViewHolder = new ViewHolder();
            localViewHolder.m_item1 = ((CircularImageView)localView.findViewById(R.id.imgDoge));
            localViewHolder.m_item2 = ((TextView)localView.findViewById(R.id.txt_profile_item)); 
            localViewHolder.ll_swipe = ((LinearLayout)localView.findViewById(R.id.ll_profile));

            localView.setTag(localViewHolder);
        }

//        localViewHolder.ll_swipe.setOnTouchListener(swipeDetector);
        //add String
        final ProfileModel t = m_lstItems.get(paramInt);
        //localViewHolder.m_item1.setImageURI(Uri.parse(t.image));

        if(t.image != null) // added this to prevent crash mail
        {
        Bitmap bm = Globals.setImageScale(con, Uri.parse(t.image));
        localViewHolder.m_item1.setImageBitmap(bm);     
        }
        String ss = t.firstName +  " " + t.lastName + " " + t.age +"years old";
        if (t.gender == 0)
        {
            ss = ss + " Male";
        }
        else
        {
            ss = ss + " Female";
        }
        localViewHolder.m_item2.setText(ss);    


        return localView;
    }

    public void update(List<ProfileModel> paramList)
    {
        this.m_lstItems = paramList;
        notifyDataSetChanged();
    }

    public class ViewHolder
    {
        public CircularImageView m_item1;
        public TextView m_item2;      
        public LinearLayout ll_swipe;
    }

    public void remove(int id) {
        // TODO Auto-generated method stub
        Log.e("LIST ITEMSL::", ""+m_lstItems);
        Log.e("mLIST ITEM SIZE::"+m_lstItems.size(), "ID>>>>"+id);
        m_lstItems.remove(m_lstItems.get(id));
        notifyDataSetChanged();
    }



}

【问题讨论】:

  • 发布您的相关代码,包括 AdapterProfileList

标签: android performance android-layout android-intent android-activity


【解决方案1】:

根据您的堆栈跟踪,您将获得一个空 uriString(显然)。

只要有可能有一个空值,你就应该有一个空检查器。

如果没有关于您的问题的更多信息,这就是我们可以向您推荐的全部内容。

【讨论】:

  • 但是什么可能触发空值?我的意思是我正在填写所有可能的字段,并且还将值从 old 更改为 new 。我注意到的是,当我在编辑后单击此保存按钮时,它会显示“更改已保存”,但是当我点击后退按钮转到上一个屏幕时应用程序崩溃。在那之后,该应用程序一直无法启动。此外,这一切都在同一个应用程序上工作。但就在早上突然停止工作。
  • t.image 绝对为空。您应该在将数据设置到适配器时跟踪为什么它为空。
【解决方案2】:

在这种错误情况下,您的图像 URI 将出现 null。为了预防,我们可以添加 Null 检查:

    if(t.image != null) {
        Bitmap bm = Globals.setImageScale(con, Uri.parse(t.image));
        localViewHolder.m_item1.setImageBitmap(bm);        
    }

这将解决上述致命错误

【讨论】:

  • 谢谢,但它导致另一个错误...现在应用程序在打开时不会崩溃,但是一旦我编辑配置文件并再次转到配置文件部分...应用程序崩溃并显示新日志。我已经粘贴了新的 logcat ....
【解决方案3】:

从您的日志中,t.image 为空,因此导致错误。你应该在调用Uri.parse()之前检查它是否为null。

至于为什么是今天而不是昨天,有很多可能:也许你已经清除了数据,也许你更新了一些你忘记的代码你已经更新了它,也许数据在你的其他部分被弄乱了应用程序。请专注于跟踪代码,而不是“为什么它今天不起作用”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多