【问题标题】:why i can't fix this error android.widget,textView cannot be cast to android.widget,button?为什么我无法修复此错误 android.widget,textView 无法转换为 android.widget,button?
【发布时间】:2014-06-11 12:41:11
【问题描述】:

这是我的第一个 android 应用程序,第一页包含 2 个按钮,第一个按钮效果很好。当我单击第二个按钮时,应用程序停止并显示错误:android.widget,textView cannot be cast to android.widget,button

如果我尝试正确读取错误,它会告诉您:Caused by... 下面的行告诉您错误是在哪里引发的,它位于line 55 上的Login.onCreate() 中。据我所知,第 55 行是:

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

我已经检查了许多类似的问题,并且在他们清理项目或正确的 XML 布局后它就消失了。 我试过: -清洁项目。 -检查过的 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

【问题讨论】:

  • 从模拟器或设备上卸载您的应用程序并重新安装。
  • 我做了,但仍然显示相同的错误
  • 那么你需要清理你的项目
  • 我清洗了几次。并且每次都显示相同的东西
  • 你试过删除gen和bin文件夹然后清理然后运行。

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


【解决方案1】:
  1. 删除您的 genbin 文件夹
  2. 清理并构建您的项目
  3. 运行您的项目

【讨论】:

  • 它应该可以工作,因为您正在访问代码中的按钮,而在您的 xml 中它是一个按钮,因此不会出现类转换异常
【解决方案2】:

从您的导入中删除行 import com.example.gestionincidents.R;。清理并运行您的项目。

【讨论】:

  • 我确实删除了该行并清理了项目,但仍然显示错误
猜你喜欢
  • 2012-09-10
  • 1970-01-01
  • 2013-01-26
  • 2011-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多