GVKun编程网logo

Android java.lang.ClassCastException:android.widget.LinearLayout无法强制转换为android.widget.TextView

15

本文将分享Androidjava.lang.ClassCastException:android.widget.LinearLayout无法强制转换为android.widget.TextView的详

本文将分享Android java.lang.ClassCastException:android.widget.LinearLayout无法强制转换为android.widget.TextView的详细内容,此外,我们还将为大家带来关于:java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to androi...、Android ClassNotFound和android.view.InflateException:、Android java.lang.ClassCastException:无法将android.widget.RelativeLayout强制转换为android.widget.EditText、android – ClassCastException LinearLayout LayoutParams的相关知识,希望对你有所帮助。

本文目录一览:

Android java.lang.ClassCastException:android.widget.LinearLayout无法强制转换为android.widget.TextView

Android java.lang.ClassCastException:android.widget.LinearLayout无法强制转换为android.widget.TextView

我正在尝试打印listView中单击的值,但后来我得到以下异常:

07-04 10:40:56.482: E/AndroidRuntime(1356): FATAL EXCEPTION: main
07-04 10:40:56.482: E/AndroidRuntime(1356): java.lang.classCastException:      android.widget.LinearLayout cannot be cast to android.widget.TextView
07-04 10:40:56.482: E/AndroidRuntime(1356):     at com.passwordkeeper.ui.ActivityHomeScreen$1.onItemClick(ActivityHomeScreen.java:88)

这是一段代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mTextView = (TextView) findViewById(R.id.labelList);
    db = new DatabaseHandler(this);
    createList();
    displayList();
}   

createList函数正常工作.这是我的displayList方法:

public void displayList(){
    this.setlistadapter(new ArrayAdapter<String>(this, R.layout.activity_home_screen, R.id.listTextView, mAccountNames));
    mListView = getListView();
    mListView.setonItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            String product = ((TextView) view).getText().toString();
            Log.v("mSelectedProduct", product);
        }           
    });
}

我的文件activity_home_screen.xml的xml代码是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
    android:id="@+id/listTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="28dp"
    android:layout_marginTop="26dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />  

任何帮助都会有用!

谢谢.

解决方法:

你可以试试这个

mListView.setonItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            TextView txtview = (TextView)view.findViewById(R.id.listTextView);
            String product = txtview.getText().toString();
            Log.v("mSelectedProduct", product);
        }           
    });

:java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to androi...

:java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to androi...

:java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams

这个错首先说明:类型转化异常

解决方法:

一、详细检查是否将某种组件强制转换为另一种类型

二、本地声明类型是否与布局文件中的一致

三、获取的布局参数的父类和布局文件不一致

总之,要细心将布局文件类型与activity中的一致。


Android ClassNotFound和android.view.InflateException:

Android ClassNotFound和android.view.InflateException:

我正在尝试将facebook整合到我的应用程序中.我使用从facebook下载的facebook示例应用程序作为参考(也在developer.facebook生成了appId和所有这个过程).我很懒,所以我不想通过所有的facebook sdk的代码.简单来说,我只是在我的应用程序中添加com_android_facebook库项目,并复制我的应用程序中的facebook示例应用程序中的所有类,Example.java除外

现在我修改MyActivity类看起来像Example.java类,意味着MyActivity现在拥有Example类的所有代码以及我的主Activity.

我改变了我的应用程序的布局

    <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout

   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   xmlns:android="http://schemas.android.com/apk/res/android"
  >
  <TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:textSize="22px"
    android:textColor="#ff00ff"
    android:gravity="center"
 >
  </TextView>

<com.android.facebook.LoginButton
 android:id="@+id/login"
 android:src="@drawable/login"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
/>

         
     
      

<Button android:id="@+id/uploadButton"
    android:text="@string/upload"
    android:visibility="invisible"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:paddingRight="20dp" 
    android:paddingLeft="20dp"
    android:layout_margin="20dp" 
    />

<Button android:id="@+id/requestButton"
    android:text="@string/request"
    android:visibility="invisible"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:paddingRight="20dp" 
    android:paddingLeft="20dp"
    android:layout_margin="20dp" 
    />


<Button android:id="@+id/postButton" 
    android:text="@string/post"
    android:visibility="invisible"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:paddingRight="20dp" 
    android:paddingLeft="20dp"
    android:layout_margin="20dp" 
    />

<Button android:id="@+id/deletePostButton" 
    android:text="@string/delete"
    android:visibility="invisible"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:paddingRight="20dp" 
    android:paddingLeft="20dp"
    android:layout_margin="20dp" 
    />

</RelativeLayout>

现在当我运行我的应用程序时,我遇到了错误

   10-14 00:58:37.786: ERROR/AndroidRuntime(3971): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.myapp/com.android.myapp.MyActivity}: android.view.InflateException: Binary XML file line #21: Error inflating class com.android.facebook.LoginButton
   10-14 00:58:37.786: ERROR/AndroidRuntime(3971): Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class com.android.facebook.LoginButton
   10-14 00:58:37.786: ERROR/AndroidRuntime(3971): Caused by: java.lang.classNotFoundException: com.android.facebook.LoginButton in loader dalvik.system.PathClassLoader@44c06850

任何帮助,建议是最受欢迎的…

解决方法:

该SDK的主要源代码中没有LoginButton.
它仅在示例代码中可用,因此它无法加载不可用的类(ClassNotFoundException)…

Android java.lang.ClassCastException:无法将android.widget.RelativeLayout强制转换为android.widget.EditText

Android java.lang.ClassCastException:无法将android.widget.RelativeLayout强制转换为android.widget.EditText

单击按钮将我从一个活动带到另一个活动时,我在Android中收到以下异常(我是Android开发中的新手,因此可能不是最聪明的问题):

java.lang.classCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText

我试过几次清理项目,尝试过Android Tools中的Fix Project Properties选项,已经检查了xml中的错误,但似乎无法弄清楚.我不得不提到,在发生此异常之前,该按钮可以正常工作一段时间.

这是我的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/editCustomerPhone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_dark"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".CustomerInfoActivity" >

<FrameLayout
    android:id="@+id/frameLayout1"
    android:layout_width="wrap_content"
    android:layout_height="250dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/ic_new_delivery" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Insert Customer Data:"
        android:textColor="@color/light_blue"
        android:textSize="20sp"
        android:text/>

</FrameLayout>

<EditText
    android:id="@+id/editCustomerName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView1"
    android:layout_alignBottom="@+id/textView1"
    android:layout_alignParentRight="true"
    android:ems="10"
    android:inputType="textPersonName"
    android:textColor="@color/light_blue" />

<Button
    android:id="@+id/submitInfoBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/editCustomerName"
    android:text="Submit Info"
    android:textColor="@color/light_red" 
    android:onClick="submitCustomerInfo"/>

<EditText
    android:id="@+id/editCustomerPhone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editCustomerName"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/TextView01"
    android:ems="10"
    android:inputType="phone"
    android:textColor="@color/light_red" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/frameLayout1"
    android:text="Name:"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@android:color/background_light"
    android:textandroid:typeface="serif" />

<TextView
    android:id="@+id/TextView02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editCustomerPhone"
    android:layout_marginTop="14dp"
    android:text="Email:"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@android:color/background_light"
    android:textandroid:typeface="serif" />

<EditText
    android:id="@+id/editCustomerEmail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/TextView02"
    android:layout_alignBottom="@+id/TextView02"
    android:layout_alignLeft="@+id/editCustomerPhone"
    android:layout_alignParentRight="true"
    android:ems="10"
    android:inputType="textEmailAddress"
    android:textColor="@color/light_green" />

<TextView
    android:id="@+id/TextView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/TextView02"
    android:layout_below="@+id/editCustomerName"
    android:layout_marginTop="19dp"
    android:text="Phone:"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@android:color/background_light"
    android:textandroid:typeface="serif" />
@H_301_17@

这是我在活动中引发异常的onCreate方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_customer_info);

    this.editMail = (EditText)findViewById(R.id.editCustomerEmail);
    this.editName = (EditText)findViewById(R.id.editCustomerName);
    this.editPhone = (EditText)findViewById(R.id.editCustomerPhone); // the exception points me here
}
@H_301_17@

解决方法:

您的代码说:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/editCustomerPhone"
@H_301_17@

editCustomerPhone是相对布局.

android – ClassCastException LinearLayout LayoutParams

android – ClassCastException LinearLayout LayoutParams

我有一个Actionbar和一些片段.问题是我的主屏幕.有一个概述左侧的片段和右侧的细节片段.我的TabListener是动态的,所以我想以编程方式设置我的布局的重量.每当我尝试获取我的LinearLayout的Layoutparams我得到一个ClassCastException:
12-22 16:00:37.620: W/System.err(7235): java.lang.classCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
12-22 16:00:37.625: W/System.err(7235):     at com.coover.eu.lostandfound.ListSearchFragment.onViewCreated(ListSearchFragment.java:184)
12-22 16:00:37.625: W/System.err(7235):     at android.support.v4.app.FragmentManagerImpl.movetoState(FragmentManager.java:884)
12-22 16:00:37.630: W/System.err(7235):     at android.support.v4.app.FragmentManagerImpl.movetoState(FragmentManager.java:1080)
12-22 16:00:37.630: W/System.err(7235):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
12-22 16:00:37.630: W/System.err(7235):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
12-22 16:00:37.630: W/System.err(7235):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:505)
12-22 16:00:37.630: W/System.err(7235):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
12-22 16:00:37.635: W/System.err(7235):     at android.app.Activity.performStart(Activity.java:5216)
12-22 16:00:37.635: W/System.err(7235):     at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:2083)
12-22 16:00:37.635: W/System.err(7235):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
12-22 16:00:37.635: W/System.err(7235):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
12-22 16:00:37.635: W/System.err(7235):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
12-22 16:00:37.640: W/System.err(7235):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 16:00:37.640: W/System.err(7235):     at android.os.Looper.loop(Looper.java:137)
12-22 16:00:37.640: W/System.err(7235):     at android.app.ActivityThread.main(ActivityThread.java:4921)
12-22 16:00:37.640: W/System.err(7235):     at java.lang.reflect.Method.invokeNative(Native Method)
12-22 16:00:37.640: W/System.err(7235):     at java.lang.reflect.Method.invoke(Method.java:511)
12-22 16:00:37.645: W/System.err(7235):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
12-22 16:00:37.645: W/System.err(7235):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
12-22 16:00:37.645: W/System.err(7235):     at dalvik.system.NativeStart.main(Native Method)

膨胀的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:id="@+id/container_found_list_overview">

    <EditText android:id="@+id/search_Box" 
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:padding="5dp"
        android:textSize="35sp"
        android:hint="@string/search"
        android:inputType="text"
        android:maxLines="1"/>
    <ListView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/overview_list_view">
    </ListView>

</LinearLayout>

代码:

LinearLayout myView = (LinearLayout)this.getView().findViewById(R.id.container_found_list_overview);
            Log.d("found container",""+(myView != null));
            try{
                LayoutParams lp = (LayoutParams) myView.getLayoutParams();
                lp.weight = 1;
                lp.width = 0;
                myView.setLayoutParams(lp);
            }
            catch(RuntimeException e){
                Log.e("cant get layoutparams",e.getMessage());
                e.printstacktrace();
            }

解决方法

发生的是一个视图的LayoutParams实际上是父类的类型.在这种情况下,您将获得一个FrameLayout $LayoutParams,因为您的LinearLayout位于FrameLayout中.

为什么是父母的类型?这是因为他们存储对父母有用的信息.如果要检索EditText或ListView的布局参数,您将获得LinearLayout $LayoutParams.

我们今天的关于Android java.lang.ClassCastException:android.widget.LinearLayout无法强制转换为android.widget.TextView的分享就到这里,谢谢您的阅读,如果想了解更多关于:java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to androi...、Android ClassNotFound和android.view.InflateException:、Android java.lang.ClassCastException:无法将android.widget.RelativeLayout强制转换为android.widget.EditText、android – ClassCastException LinearLayout LayoutParams的相关信息,可以在本站进行搜索。

本文标签: