Android:异步任务崩溃应用程序

Android:异步任务崩溃应用程序,android,android-asynctask,Android,Android Asynctask,我的申请有问题。 我想显示用户的个人资料,我的应用程序中有两个链接 一个链接是通过TextView,它运行showUser(视图v)方法: public void showUser(View v){ Intent i; i=new Intent(getApplicationContext(), ShowProfile.class); i.putExtra("id",user); // user is String with users ID startActi

我的申请有问题。 我想显示用户的个人资料,我的应用程序中有两个链接

一个链接是通过TextView,它运行showUser(视图v)方法:

public void showUser(View v){
    Intent i;

    i=new Intent(getApplicationContext(), ShowProfile.class);
    i.putExtra("id",user); // user is String with users ID

    startActivity(i);
}
第二个链接位于对话框中,用户可以打开该对话框: (我将在这里发布整个方法,但我将强调重要部分)

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder .setTitle(R.string.show_photo_show_rated_users_title)
            .setNegativeButton("Close", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {
                       dialog.dismiss();
                   }
             });

    ListView modeList = new ListView(this);
    String[] stringArray = new String[ratedUsers.size()];

    for ( int i=0 ; i<ratedUsers.size() ; i++ ){
        stringArray[i] = ratedUsers.get(i).get("name");
    }

    ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, R.layout.dropdown_item_white, android.R.id.text1, stringArray);
    modeList.setAdapter(modeAdapter);
    modeList.setOnItemClickListener(new ListView.OnItemClickListener(){

/*********************** IMPORTANT PART *********************************/

        @Override
        public void onItemClick(AdapterView<?> parent, View arg1, int index,long arg3) { 
            Intent i;

            i=new Intent(ShowPhotoDetails.this , ShowProfile.class);
            i.putExtra("id",ratedUsers.get(index).get("id"));

            /**** ratedUsers is ArrayList<HashMap<String,String>> ****/


            startActivity(i);
        }});

    builder.setView(modeList);
    final Dialog dialog = builder.create();

    dialog.show();
}
我想知道为什么在一种情况下它运行得很好,而在另一种情况下它崩溃了。正如我在代码中所写,这次崩溃没有LogCat输出。它甚至没有说未捕获异常或类似的内容

编辑:我发现了错误的原因

public class GetUserInformations extends AsyncTask<String,Void,Void>{
    Map<String,Object> tmpUser;
    @Override
    protected void onPreExecute(){
        tmpUser = new HashMap<String,Object>();
    }

    @Override
    protected Void doInBackground(String... arg) {
        try{
            int u_id = Integer.parseInt(arg[0]);
            tmpUser = myDb.getUser(u_id); // downloading info
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void arg){
        if ( tmpUser != null ){

            Log.w("LOG",""+tmpUser.get("name"));

            name = (String) tmpUser.get("name");
            fbId = (String) tmpUser.get("id");
            email = (String) tmpUser.get("email");
            country = (Integer) tmpUser.get("country");

            userName.setText(name);
            profilepic.setProfileId(fbId);

            userSubscribe.setVisibility(View.VISIBLE);
        }
        else {
            Toast.makeText(getBaseContext(), "Error", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

}
public类GetUserInformations扩展异步任务{
tmpUser地图;
@凌驾
受保护的void onPreExecute(){
tmpUser=newhashmap();
}
@凌驾
受保护的Void doInBackground(字符串…arg){
试一试{
int u_id=Integer.parseInt(arg[0]);
tmpUser=myDb.getUser(u_id);//下载信息
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(void arg){
if(tmpUser!=null){
Log.w(“Log”,“tmpUser.get”(“name”);
name=(字符串)tmpUser.get(“name”);
fbId=(字符串)tmpUser.get(“id”);
email=(字符串)tmpUser.get(“email”);
country=(整数)tmpUser.get(“country”);
userName.setText(name);
profilepic.setProfileId(fbId);
userSubscribe.setVisibility(View.VISIBLE);
}
否则{
Toast.makeText(getBaseContext(),“Error”,Toast.LENGTH_SHORT.show();
完成();
}
}
}
当我第一次打开“活动”时,所有内容都可以正常下载,但当我再次按下并单击指向此活动的链接时,它会给我NullPointerException


你知道为什么吗?

在你的onItemClick函数中,试着放:

而不是:

删除此项:

 new GetUserInformations().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, id);
和使用:

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
    new GetUserInformations().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, id);
}
else {
    new GetUserInformations().execute(id);
}

您面临此问题的API级别是什么。以蜂巢为参考,尝试在不同级别上运行它

需要检查相同项并应用execute或executeONExecutor,如下所示:

if (currentApiVersion >=
                android.os.Build.VERSION_CODES.HONEYCOMB) {
            new YourAsynctask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
           } else {
               new YourAsynctask().execute();
           }  

我想知道您的日志在哪里错误是什么,以及堆栈跟踪。这通常会告诉你从哪里开始。它在哪一行崩溃?如果你读了第二个代码,你们两个都会得到答案。你知道错误抛出的地方吗?它在哪条线上运行然后崩溃?下面是它的具体操作:
 new GetUserInformations().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, id);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
    new GetUserInformations().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, id);
}
else {
    new GetUserInformations().execute(id);
}
if (currentApiVersion >=
                android.os.Build.VERSION_CODES.HONEYCOMB) {
            new YourAsynctask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
           } else {
               new YourAsynctask().execute();
           }