簡體   English   中英

嘗試登錄時Android App崩潰

[英]Android App crashing while trying to login

嗨,我是android world的新手。 我正在嘗試為我的應用程序創建一個登錄頁面,該頁面將通過editext獲取用戶ID和密碼,並將其發布在url中以進行登錄身份驗證。 我已經編寫了以下代碼(源Internet),但是我的應用程序崩潰了。 我還想檢查身份驗證是否成功,但是由於我的應用崩潰了,我無法走得更遠。 有人可以幫我嗎。 以下是我的應用程序的代碼。 謝謝。

import java.io.BufferedReader;
import java.io.IOException; 
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;   
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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 android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Login extends Activity {

    Button ok,back,exit;
    TextView result;
    String tag= "abc";

        /** Called when the activity is first created. */

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

        public void postLoginData(String username, String password ) {
            // Create a new HttpClient and Post Header

            HttpClient httpclient = new DefaultHttpClient();


            HttpPost httppost = new HttpPost("(my url)");

            try {
                // Add user name and password


                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("username", username));
                nameValuePairs.add(new BasicNameValuePair("password", password));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                Log.i(tag, "name and password");
                // Execute HTTP Post Request
                Log.w(tag, "Execute HTTP Post Request");
                HttpResponse response = httpclient.execute(httppost);

                String str = InputStreamToString(response.getEntity().getContent()).toString();
} catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            } catch (IOException e) {
        e.printStackTrace();
            }
}


private StringBuilder inputStreamToString(InputStream is) {
        String line = "";
        StringBuilder total = new StringBuilder();
        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        // Read response until the end
        try {
        while ((line = rd.readLine()) != null) { 
            total.append(line); 
        }
        } catch (IOException e) {
        e.printStackTrace();
        }
        // Return full string
        return total;
        }


LOGCAT:

01-02 09:13:14.802: W/dalvikvm(2933): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
01-02 09:13:14.951: E/AndroidRuntime(2933): FATAL EXCEPTION: main
01-02 09:13:14.951: E/AndroidRuntime(2933): java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
01-02 09:13:14.951: E/AndroidRuntime(2933):     at com.example.project1.MainActivity$1.onClick(MainActivity.java:40)
01-02 09:13:14.951: E/AndroidRuntime(2933):     at android.view.View.performClick(View.java:4202)
01-02 09:13:14.951: E/AndroidRuntime(2933):     at android.view.View$PerformClick.run(View.java:17340)
01-02 09:13:14.951: E/AndroidRuntime(2933):     at android.os.Handler.handleCallback(Handler.java:725)
01-02 09:13:14.951: E/AndroidRuntime(2933):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-02 09:13:14.951: E/AndroidRuntime(2933):     at android.os.Looper.loop(Looper.java:137)
01-02 09:13:14.951: E/AndroidRuntime(2933):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-02 09:13:14.951: E/AndroidRuntime(2933):     at java.lang.reflect.Method.invokeNative(Native Method)
01-02 09:13:14.951: E/AndroidRuntime(2933):     at java.lang.reflect.Method.invoke(Method.java:511)
01-02 09:13:14.951: E/AndroidRuntime(2933):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-02 09:13:14.951: E/AndroidRuntime(2933):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-02 09:13:14.951: E/AndroidRuntime(2933):     at dalvik.system.NativeStart.main(Native Method)
01-02 09:13:15.191: W/ActivityManager(278):   Force finishing activity com.example.project1/.MainActivity


This is MainActivity:

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public  class MainActivity extends Activity  {

Button ok,back,exit;

String tag= "Project 1";

    /** Called when the activity is first created. */
    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @SuppressLint("NewApi")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();                 
        StrictMode.setThreadPolicy(policy);             
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Login button clicked
        ok = (Button)findViewById(R.id.b);
        ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                EditText uname = (EditText)findViewById(R.id.id);
                String username = uname.getText().toString();

                EditText pword = (EditText)findViewById(R.id.psswd);
                String password = pword.getText().toString();
                Login l = new Login();
                l.postLoginData(username, password);


            }
        });
    }
}

您的例外說明了一切。

java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText

您在xml布局中擁有的是TextView ,而不是EditText

需要確保看到布局XML,但是在這里,您正在轉換為EditText。 我敢打賭,您的布局XML將id和psswd中的一個或兩個都聲明為TextView:

EditText uname = (EditText)findViewById(R.id.id);
String username = uname.getText().toString();

EditText pword = (EditText)findViewById(R.id.psswd);
String password = pword.getText().toString();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM