为什么我无法解决这个错误android.widget,textView无法强制转换为android.widget,按钮?

时间:2014-06-11 12:41:12

标签: android xml android-layout textview android-button

这是我的第一个android应用程序,第一个页面包含2个按钮,第一个按钮工作得相当不错。当我点击第二个按钮时,应用程序停止并显示错误:android.widget,textView cannot be cast to android.widget,button

如果我尝试正确阅读错误,则告诉您:Caused by...下方的行会告诉您错误发生的位置,Login.onCreate()位于line 55上。我最好能告诉第55行:

btnLogin = (Button) findViewById(R.id.btnFerme);

我已经检查了许多类似的问题,并且在清理项目或更正XML布局后它已经消失了。 我尝试过: - 清洁项目。 -chekcked XML文件。 - 使用XML onClick属性创建另一个方法 而且它还没有奏效。有人可以请帮帮我吗?谢谢。

XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/in" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="10dip" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dip"
            android:layout_marginTop="20dp"
            android:text="Authentification"
            android:textColor="#ffffff"
            android:textSize="25sp"
            android:typeface="serif" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="20dp"
            android:text="Login"
            android:textColor="#3b3b3b"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/txt_login"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:hint="Entrer votre login"
            android:inputType="textEmailAddress" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="15dip"
            android:text="Mot de passe"
            android:textColor="#3b3b3b"
            android:textSize="20sp"
            android:textStyle="normal" />

        <EditText
            android:id="@+id/txt_mdp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:hint="Entrer votre mot de passe"
            android:inputType="textPassword" />

        <Button
            android:id="@+id/btnFerme"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:layout_marginTop="40dp"
            android:text="Se connecter"
            android:textSize="18sp" />
    </LinearLayout>



</ScrollView>

编辑:

 package com.example.gestionincidents;

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

    import org.apache.http.HttpEntity;
    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.app.ProgressDialog;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;

    import com.example.gestionincidents.Login;
    import com.example.gestionincidents.R;



    public class Login extends Activity {
        private static final String strURL = "http://unfmtetouan.com/android_connect/utilisateur.php";
        private EditText inputEmail;
        private EditText inputPassword;
        private Button btnLogin;

        private ProgressDialog pDialog;
        private AlertDialogManager alert = new AlertDialogManager();
        private static final String ExtraLogin = "Login";
        protected static final String ExtraUser = "UserKey";



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

            inputEmail = (EditText) findViewById(R.id.txt_login);
            inputPassword = (EditText) findViewById(R.id.txt_mdp);
            btnLogin = (Button) findViewById(R.id.btnFerme);

            btnLogin.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    new Compte().execute();

                }
                });



        }
            class Compte extends AsyncTask<String,String, String> {

                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    pDialog = new ProgressDialog(Login.this);
                    pDialog.setMessage("Veuillez Patienter ...");
                    pDialog.setIndeterminate(false);
                    pDialog.setCancelable(true);
                    pDialog.show();

                }


                @Override
                protected String doInBackground(String... arg0) {
                    String result =null;
                    InputStream is = null;
                    StringBuilder sb = new StringBuilder();
                    ArrayList<NameValuePair> nameValuePairs=new ArrayList<NameValuePair> ();
                    nameValuePairs.add(new BasicNameValuePair("utilisateur",inputEmail.getText().toString()));

                    try{

                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost(strURL);
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                        HttpResponse response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();
                        is = entity.getContent();    
                    }catch(Exception exp){
                        Log.e("log_tag", "Error in http connection " + exp.toString());
                    } 
                    try{
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);

                        String line = null;
                        while ((line = reader.readLine()) != null) {
                            sb.append(line + "\n");

                        }
                        is.close();
                        result=sb.toString();

                    }catch(Exception exc){
                        Log.e("log_tag", "Error in http connection " + exc.toString());
                    }
                    String essai=result.substring(0, 4) ;
                    try {
                        if (result.matches("<br >")){
                             essai=result.substring(0, 2) ; 
                        }
                        JSONArray jArray = new JSONArray(result);
                        int b=jArray.length();



                           JSONObject json_data = jArray.getJSONObject(0);


                           final String mot_pass = json_data.getString("mdp").toString();
                           final String login = json_data.getString("Login").toString();
                           final String idu = json_data.getString("id_utilisateur").toString();
                           new Thread(new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                if (mot_pass.equals(inputPassword.getText().toString())){

                                       Intent principale =new Intent(Login.this, afficher_incidents.class);
                                       principale.putExtra(ExtraLogin, login);
                                       principale.putExtra(ExtraUser, idu);
                                       startActivity(principale);

                                       finish();

                                   }else {
                                       runOnUiThread(new Runnable() { 
                                            public void run() {
                                                alert.showAlertDialog(Login.this, ":(" , "Mot de Passe Incorrecte !" , false);
                                //  hada howa l3adi         Toast.makeText(getApplicationContext(), "Mots de Passe Incorrect", Toast.LENGTH_SHORT).show();
                                            }
                                        });
                                      }
                            }
                        }).start();


                   }catch(JSONException e){
                       runOnUiThread(new Runnable() {
                            public void run() {
                                alert.showAlertDialog(Login.this, ":(" , "Login Incorrecte !" , false);
                            }
                        });

                   }


                    // TODO Auto-generated method stub
                    return null;
                }

                @Override
                protected void onPostExecute(String file_url) {
                    // dismiss the dialog once done
                    pDialog.dismiss();
                }
    }}

编辑2:

06-11 13:03:52.921: E/AndroidRuntime(5877): FATAL EXCEPTION: main
06-11 13:03:52.921: E/AndroidRuntime(5877): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.catours.tourfinder/com.example.gestionincidents.Login}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
06-11 13:03:52.921: E/AndroidRuntime(5877):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
06-11 13:03:52.921: E/AndroidRuntime(5877):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
06-11 13:03:52.921: E/AndroidRuntime(5877):     at android.app.ActivityThread.access$700(ActivityThread.java:158)
06-11 13:03:52.921: E/AndroidRuntime(5877):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
06-11 13:03:52.921: E/AndroidRuntime(5877):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 13:03:52.921: E/AndroidRuntime(5877):     at android.os.Looper.loop(Looper.java:176)
06-11 13:03:52.921: E/AndroidRuntime(5877):     at android.app.ActivityThread.main(ActivityThread.java:5365)
06-11 13:03:52.921: E/AndroidRuntime(5877):     at java.lang.reflect.Method.invokeNative(Native Method)
06-11 13:03:52.921: E/AndroidRuntime(5877):     at java.lang.reflect.Method.invoke(Method.java:511)
06-11 13:03:52.921: E/AndroidRuntime(5877):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-11 13:03:52.921: E/AndroidRuntime(5877):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-11 13:03:52.921: E/AndroidRuntime(5877):     at dalvik.system.NativeStart.main(Native Method)
06-11 13:03:52.921: E/AndroidRuntime(5877): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
06-11 13:03:52.921: E/AndroidRuntime(5877):     at com.example.gestionincidents.Login.onCreate(Login.java:55)
06-11 13:03:52.921: E/AndroidRuntime(5877):     at android.app.Activity.performCreate(Activity.java:5326)
06-11 13:03:52.921: E/AndroidRuntime(5877):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
06-11 13:03:52.921: E/AndroidRuntime(5877):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
06-11 13:03:52.921: E/AndroidRuntime(5877):     ... 11 more

2 个答案:

答案 0 :(得分:0)

  1. 删除 gen bin 文件夹
  2. 清理并构建项目
  3. 运行您的项目

答案 1 :(得分:0)

从import.Clean删除行import com.example.gestionincidents.R;并运行您的项目。