Android:AsyncTask崩溃的应用程序

时间:2014-08-07 12:06:46

标签: android android-asynctask

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

一个链接是通过TextView运行 showUser(View 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();
}

最后是 ShowProfile.class

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.profile);

    Intent i = getIntent();

    try {
        id = i.getStringExtra("id");
    }catch(Exception e){
        e.printStackTrace();
        Toast.makeText(getBaseContext(), "Error loading intent", Toast.LENGTH_SHORT).show();
        finish();
    }

    try{
        Log.w("ID",id); //always give right number
        new GetUserInformations().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, id);

        /* 
           If I comments this asyncTask, there's no error at all, but if I run it, It
           open debug View in Eclipse, says that "Source not found" and crashes...
           No LogCat Output
        */
    }catch(Exception e){
        e.printStackTrace();
    }
...

我想知道为什么在一个案例中它运行得很好而在另一个案例中它崩溃了。正如我在代码中所写,这次崩溃没有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();
        }
    }

}

当我第一次打开活动时,一切都下载得很好,但是当我再次点击并点击链接到此活动时,它会给我NullPointerException。

你知道为什么吗?

3 个答案:

答案 0 :(得分:0)

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

i = new Intent(getApplicationContext(), ShowProfile.class);

代替:

i = new Intent(ShowPhotoDetails.this, ShowProfile.class);

答案 1 :(得分:0)

删除它:

 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);
}

答案 2 :(得分:0)

您遇到此问题的API级别是什么?尝试在不同的级别上运行它,以Honeycomb作为参考。

需要检查相同的内容并执行execute或executeONExecutor,如下所示:

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

Check this asynctask-threading-regression-confirmed blog post