从Android中的AsyncTask(PostExecute)启动Activiy

时间:2015-08-27 13:07:02

标签: android android-intent android-activity android-asynctask android-context

我读了很多关于它的问题,任何人都解决了我的问题。我有一个AsyncTask,必须启动一个新的活动。代码如下:

public class UpdateCatalog extends AsyncTask <Void,Integer,Void> {

private Context context;
private LoadActivity activity;

public UpdateCatalog(LoadActivity activity) {
    super();
    this.activity = activity;
    this.context = this.activity.getApplicationContext();
} 
   .
   .
   .

@Override
protected void onPostExecute(final Void result) {
    // Update your views here
    LoadActivity.progressStatus.setVisibility(View.GONE);
    context.startActivity(new Intent(context, DownloadImages.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
}

对此AsincTask的调用是在启动AsincTask的Activity中,如下所示:

   public class LoadActivity extends AppCompatActivity {

public static TextView txtStatus;
public static ProgressBar progressStatus;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_load);
    txtStatus  = (TextView) findViewById(R.id.progressStatus);
    progressStatus = (ProgressBar) findViewById(R.id.progressBar);
    if(CheckCatalog()){
        ShowUpdateDialog();
    }
    else
        new UpdateCatalog(this).execute();
}

错误如下:

    FATAL EXCEPTION: main
android.content.ActivityNotFoundException: Unable to find explicit activity class {es.aplicaciones.alvaro.entrelazadas/es.aplicaciones.alvaro.entrelazadas.DownloadImages};

已经创建了DownloadImages.class。所以我认为这不是问题。我尝试用其他方式将上下文作为参数,但它仍然不起作用。 请你能帮帮我吗?

2 个答案:

答案 0 :(得分:1)

请记住,您必须在def verifylogin(): fin=open("moosebook.txt","r") data=fin.readlines() for line in data: fields=line.split() fields=[i.rstrip("','") for i in fields] #strips the named character from END of field fields=[i.replace("'",'') for i in fields]#when reading the list, you want to remoe the ',' so it isn't part of the username or password fields=[i.replace("(",'') for i in fields] #simiarly, remove the bracket and replace it line=line.rstrip() print(fields) access_permitted = False for counter in range(0,len(fields)): if textlogin.get()==fields[counter] and textpassword.get()==fields[counter+1]: access_permitted=True if access_permitted: welcome=Label(myGui,text="Access Granted. Loading Profile ....") welcome.pack() else: denied=Label(myGui,text="Access Denied") denied.pack() 文件中声明每个活动:)

AndroidManifest.xml

答案 1 :(得分:1)

检查您的清单文件。 如果您的DownloadImages课程位于&#34;基地&#34;在清单文件中声明的包然后使用 .ClassName

添加活动
<application> 
....
     <activity android:name=".DownloadImages" />
</application>

否则  写下DownloadImages课程的完整路径。

 <application> 
    ....
         <activity android:name="com.example.path.DownloadImages" />
 </application>