Android Studio - 从OnPostExecute()调用的新活动无法打开

时间:2017-04-28 19:02:36

标签: android

我已经在这个问题上工作了几个小时而没有成功。我还引用了其他StackOverflow问题而未找到解决方案。

当我从异步任务onPostExecute()方法调用我的新活动时,新活动无法打开。它列在我的清单中。按钮单击但屏幕不会切换到新活动。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_homepage);

    Button editprofile = (Button) findViewById(R.id.profilebutton);
    editprofile.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            new ProfileTask().execute();
        }
    });


    //search = (EditText)findViewById(R.id.editText); //setup variable
}

public void Go(View view) {

}

//public void Edit(View view) {
    //this.startActivity(new Intent(this, ProfileActivity.class));
//}

public void Viewcart(View view) {
    //
}

public void Logout(View view) {
    setContentView(R.layout.activity_main);
}

private class ProfileTask extends AsyncTask<String, Void, String> {
    private Context context;

    protected void onpreExecute() {} //parent class

    @Override //do not want to call parent method. use super if you want both
    protected String doInBackground(String... arg0) {
        try {
            //search for phrase
            //php script to call
            String link = "http://people.aero.und.edu/~lwingate/457/2/profile_get.php";

            //get method to pass variables
            link += "?username=" + URLEncoder.encode("customer", "UTF-8");
            link += "&password=" + URLEncoder.encode("testing", "UTF-8");

            //Connect to server
            URL url = new URL(link);
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);

            //Read server response
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                break;
            }
            return sb.toString();
        }
        catch(Exception e) {
            return new String("Exception: " + e.getMessage());
        }
    }

    @Override
    protected void onPostExecute(String result) { //result of background computation passed here after doneif (result!=null) { //if returns with data, entry exists. login successful
        Intent intent = new Intent(context, ProfileResultsActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

        //Intent newintent = new Intent();
        //newintent.setClass(context, SearchResultsActivity.class);
        //context.startActivity(newintent);
        //context.startActivity(new Intent(MainActivity.this, SearchResultsActivity.class));
    }
}

}

LogCat:
04-28 21:21:27.055 29642-29642/? I/art: Not late-enabling -Xcheck:jni (already on)
04-28 21:21:27.055 29642-29642/? W/art: Unexpected CPU variant for X86 using defaults: x86
04-28 21:21:27.183 29642-29642/com.example.aplicacaodelivros W/System: ClassLoader referenced unknown path: /data/app/com.example.aplicacaodelivros-2/lib/x86
04-28 21:21:27.201 29642-29642/com.example.aplicacaodelivros I/InstantRun: starting instant run server: is main process
04-28 21:21:27.337 29642-29670/com.example.aplicacaodelivros I/OpenGLRenderer: Initialized EGL, version 1.4
04-28 21:21:27.337 29642-29670/com.example.aplicacaodelivros D/OpenGLRenderer: Swap behavior 1
04-28 21:21:27.338 29642-29670/com.example.aplicacaodelivros W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
04-28 21:21:27.338 29642-29670/com.example.aplicacaodelivros D/OpenGLRenderer: Swap behavior 0
04-28 21:21:29.761 29642-29642/com.example.aplicacaodelivros W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
04-28 21:21:30.695 29642-29642/com.example.aplicacaodelivros W/IInputConnectionWrapper: finishComposingText on inactive InputConnection

2 个答案:

答案 0 :(得分:0)

请尝试此操作,如果不能正常显示您的logcat。

Intent intent = new Intent(context, ProfileResultsActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

 Change your code below this

 Intent intent = new Intent(Yourclassnname.this,ProfileResultsActivity.class);
 startActivity(intent);

答案 1 :(得分:0)

private class ProfileTask extends AsyncTask<String, Void, String> { private Context context; protected void onpreExecute(Context context) {this.context=context;
} 
onpostexecute方法中的

将其写为

protected void onPostExecute(String result)  { Intent intent = new Intent(context, ProfileResultsActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context. startActivity(intent);
相关问题