我怎样才能改变onpostexecute Android上的活动

时间:2014-02-18 11:35:07

标签: java android

所以我试图搜索stackoverflow,但没有一个解决方案对我有用。我想从服务器获取数据,所以我发布请求,然后如果结果为真,我需要更改活动。

package com.example.earchive;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends Activity {




    public String propertyNumber;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.login, menu);
/*** Work with login button ***/        
        final Button button = (Button) findViewById(R.id.login_bt);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                FetchTask fetch = new FetchTask();

                EditText email = (EditText)findViewById(R.id.email);
                fetch.email =  email.getText().toString();
                EditText password = (EditText)findViewById(R.id.password);
                fetch.password =  password.getText().toString();
                fetch.v = v;
                fetch.execute();

            }
        });
/*** End of work with login button ***/         


        return true;


    } 


    public class FetchTask extends AsyncTask<Void, Void, JSONArray> {

        public JSONArray result_arr;
        public String result_str,email,password,test;
        public boolean is_loged;
        public View v;
        @Override

        protected JSONArray doInBackground(Void... params) {
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("HERE MY LINK");

                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);



                nameValuePairs.add(new BasicNameValuePair("em", this.email));
                nameValuePairs.add(new BasicNameValuePair("pa", this.password));
                nameValuePairs.add(new BasicNameValuePair("os", "android"));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);

                BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "utf-8"), 8);
                StringBuilder sb = new StringBuilder();
                sb.append(reader.readLine());
                String line = "0";
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
                reader.close();
                String result11 = sb.toString();

                this.result_str = result11;
                // parsing data
                JSONArray arr = new JSONArray(result11);




                return new JSONArray(result11);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        protected void onPostExecute(JSONArray result) {

            int auth;
            if (result != null) {
                JSONObject json_data;
                try {
                    json_data = result.getJSONObject(0);
                    auth = json_data.getInt("auth");

                    if(auth != 0)
                    {
                        startActivity(new Intent(LoginActivity.this, InboxActivity.class));  


                    }
                    else
                    {

                    }
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }

            } else {
                System.out.println("error occured");

            }
        }
    }
}

我的InboxActivity类

package com.example.earchive;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class InboxActivity extends Activity {

    public String propertyNumber;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inbox);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.login, menu);
        return true;    
    }    
}

这是我的AndroidManifest代码(res / AndroidManifest.xml)

这是来自res / AndroidManifest.xml的代码

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.earchive"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.earchive.LoginActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我的错误记录:

02-18 06:45:26.611: E/AndroidRuntime(1814): FATAL EXCEPTION: main
02-18 06:45:26.611: E/AndroidRuntime(1814): Process: com.example.earchive, PID: 1814
02-18 06:45:26.611: E/AndroidRuntime(1814): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.earchive/com.example.earchive.InboxActivity}; have you declared this activity in your AndroidManifest.xml?
02-18 06:45:26.611: E/AndroidRuntime(1814):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
02-18 06:45:26.611: E/AndroidRuntime(1814):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
02-18 06:45:26.611: E/AndroidRuntime(1814):     at android.app.Activity.startActivityForResult(Activity.java:3424)
02-18 06:45:26.611: E/AndroidRuntime(1814):     at android.app.Activity.startActivityForResult(Activity.java:3385)
02-18 06:45:26.611: E/AndroidRuntime(1814):     at android.app.Activity.startActivity(Activity.java:3627)
02-18 06:45:26.611: E/AndroidRuntime(1814):     at android.app.Activity.startActivity(Activity.java:3595)
02-18 06:45:26.611: E/AndroidRuntime(1814):     at com.example.earchive.LoginActivity$FetchTask.onPostExecute(LoginActivity.java:155)
02-18 06:45:26.611: E/AndroidRuntime(1814):     at com.example.earchive.LoginActivity$FetchTask.onPostExecute(LoginActivity.java:1)
02-18 06:45:26.611: E/AndroidRuntime(1814):     at android.os.AsyncTask.finish(AsyncTask.java:632)
02-18 06:45:26.611: E/AndroidRuntime(1814):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-18 06:45:26.611: E/AndroidRuntime(1814):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
02-18 06:45:26.611: E/AndroidRuntime(1814):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-18 06:45:26.611: E/AndroidRuntime(1814):     at android.os.Looper.loop(Looper.java:136)
02-18 06:45:26.611: E/AndroidRuntime(1814):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-18 06:45:26.611: E/AndroidRuntime(1814):     at java.lang.reflect.Method.invokeNative(Native Method)
02-18 06:45:26.611: E/AndroidRuntime(1814):     at java.lang.reflect.Method.invoke(Method.java:515)
02-18 06:45:26.611: E/AndroidRuntime(1814):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-18 06:45:26.611: E/AndroidRuntime(1814):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-18 06:45:26.611: E/AndroidRuntime(1814):     at dalvik.system.NativeStart.main(Native Method)

当我运行我的应用程序时,当我发送帖子请求时,我看到错误"Unfortunately, APPNAME has stopped"

我需要做什么?

感谢所有人!

1 个答案:

答案 0 :(得分:0)

添加

 <activity android:name="com.example.earchive.InboxActivity">
    </activity>
<{1}}

application标记中的