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

时间:2017-07-11 09:28:43

标签: java android textview

我该如何解决这个问题?

logcat的:

FATAL EXCEPTION: main
 Process: com.example.vicevirus.myapplication, PID: 10011
 java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
     at com.example.vicevirus.myapplication.MainActivity$2.populateView(MainActivity.java:66)
     at com.example.vicevirus.myapplication.MainActivity$2.populateView(MainActivity.java:63)
     at com.firebase.ui.database.FirebaseListAdapter.getView(FirebaseListAdapter.java:135)
     at android.widget.AbsListView.obtainView(AbsListView.java:2263)
     at android.widget.ListView.makeAndAddView(ListView.java:1790)
     at android.widget.ListView.fillDown(ListView.java:691)
     at android.widget.ListView.fillFromTop(ListView.java:752)
     at android.widget.ListView.layoutChildren(ListView.java:1616)
     at android.widget.AbsListView.onLayout(AbsListView.java:2091)
     at android.view.View.layout(View.java:14817)
     at android.view.ViewGroup.layout(ViewGroup.java:4631)
     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
     at android.view.View.layout(View.java:14817)
     at android.view.ViewGroup.layout(ViewGroup.java:4631)
     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
     at android.view.View.layout(View.java:14817)
     at android.view.ViewGroup.layout(ViewGroup.java:4631)
     at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
     at android.view.View.layout(View.java:14817)
     at android.view.ViewGroup.layout(ViewGroup.java:4631)
     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
     at android.view.View.layout(View.java:14817)
     at android.view.ViewGroup.layout(ViewGroup.java:4631)
     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
     at android.view.View.layout(View.java:14817)
     at android.view.ViewGroup.layout(ViewGroup.java:4631)
     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
     at android.view.View.layout(View.java:14817)
     at android.view.ViewGroup.layout(ViewGroup.java:4631)
     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2011)
     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1768)
     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1024)
     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5694)
     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
     at android.view.Choreographer.doFrame(Choreographer.java:544)
     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
     at android.os.Handler.handleCallback(Handler.java:733)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:136)
     at android.app.ActivityThread.main(ActivityThread.java:5019)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:515)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
     at dalvik.system.NativeStart.main(Native Method)

MainActivity.java:

package com.example.vicevirus.myapplication;

import android.app.Application;
import android.content.Context;
import android.os.*;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.auth.ui.User;
import com.firebase.ui.database.FirebaseListAdapter;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {

    private EditText editText;
    private DatabaseReference chat_data_ref;
    private DatabaseReference user_name_ref;
    private ListView listView;
    private FirebaseAuth mAuth;
    private String name="";
    private static final String TAG = "RegisterActivity";
    HashMap<String,String> map;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAuth= FirebaseAuth.getInstance();
        editText=(EditText)findViewById(R.id.edittext);
        chat_data_ref= FirebaseDatabase.getInstance().getReference().child("chat_data");
        user_name_ref=FirebaseDatabase.getInstance().getReference().child("chat_users").child(mAuth.getCurrentUser().getUid()).child("name");
        listView=(ListView)findViewById(R.id.listview);
        map=new HashMap<>();
        FirebaseAuth auth = FirebaseAuth.getInstance();
        FirebaseAuth.AuthStateListener mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    // User is signed in
                    Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
                } else {
                    // User is signed out
                    Log.d(TAG, "onAuthStateChanged:signed_out");
                }
            }
        };
        FirebaseListAdapter<Message> adapter=new FirebaseListAdapter<Message>(
                this,Message.class,R.layout.individual_row,chat_data_ref
        ) {
            @Override
            protected void populateView(View v, Message model, int position) {
                TextView msg=(TextView)v.findViewById(R.id.textView1);
                msg.setText(model.getUser_name()+" : "+model.getMessage());
            }
        };
        listView.setAdapter(adapter);
    }
    public void send(View view) {
        chat_data_ref.push().setValue(new Message(editText.getText().toString(),name));//storing actual msg with name of the user
        editText.setText("");//clear the msg in edittext
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==R.id.logout)
        {
            mAuth.signOut();//when the user clicks signout option this will executes
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}

我的个人_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:baselineAligned="false"
        android:orientation="horizontal"
        android:padding="2dp" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:gravity="top|center" >

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="vertical" >
                <TextView
        android:text="TextView"
        android:textStyle="bold"
        android:layout_width="match_parent"
        android:textSize="20sp"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:layout_gravity="bottom"
        android:background="@drawable/ute"
                />
          </RelativeLayout>
        </RelativeLayout>
<LinearLayout
    android:layout_width="0dip"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="@drawable/ic_leq"
    android:gravity="top|center"
    android:orientation="vertical" >
    </LinearLayout>
</LinearLayout>
</LinearLayout>

请帮帮我。

4 个答案:

答案 0 :(得分:1)

您的ID textview1加倍,只需从线性布局中移除 android:id="@+id/textView1"

 <LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"

   <TextView
    android:text="TextView"
    android:textStyle="bold"
    android:layout_width="match_parent"
    android:textSize="20sp"
    android:layout_height="wrap_content"
    android:id="@+id/textView1"

答案 1 :(得分:0)

你的id textview1加倍,它被设置为线性布局和textview 改变其中一个

答案 2 :(得分:0)

您有2个具有相同ID的视图,下次仔细阅读logcat

detector->detect(img1, kp1,descriptors_img1);
detector->detect(img2, kp2,descriptors_img2);


Ptr<DescriptorExtractor> extractor = ORB::create();
extractor->compute(img1, kp1, descriptors_img1 );
extractor->compute(img2, kp2, descriptors_img2 );

if ( descriptors_img1.empty() )
    cvError(0,"MatchFinder","1st descriptor empty",__FILE__,__LINE__);
if ( descriptors_img2.empty() )
    cvError(0,"MatchFinder","2nd descriptor empty",__FILE__,__LINE__);
descriptors_img1.convertTo(descriptors_img1, CV_32F);
descriptors_img2.convertTo(descriptors_img2, CV_32F);
FlannBasedMatcher matcher;
std::vector<DMatch> matches;
matcher.match(descriptors_img1,descriptors_img2,matches);
double max_dist = 0; double min_dist = 100;

//-- Quick calculation of max and min distances between keypoints
for( int i = 0; i < descriptors_img1.rows; i++ )
{
    double dist = matches[i].distance;
    if( dist < min_dist )
        min_dist = dist;
    if( dist > max_dist )
        max_dist = dist;
}

printf("-- Max dist : %f \n", max_dist );
printf("-- Min dist : %f \n", min_dist );

//-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
std::vector< DMatch > good_matches;

for( int i = 0; i < descriptors_img1.rows; i++ )
{
    if( matches[i].distance < 3*min_dist )
    {
        good_matches.push_back( matches[i]);
    }
}
Mat img_matches;
drawMatches(img1,kp1,img2,kp2,good_matches,img_matches,Scalar::all(-1),
        Scalar::all(-1),vector<char>(),DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
//-- Localize the object
std::vector<Point2f> obj;
std::vector<Point2f> scene;
for( int i = 0; i < good_matches.size(); i++ )
{
    //-- Get the keypoints from the good matches
    obj.push_back( kp1[ good_matches[i].queryIdx ].pt );
    scene.push_back( kp2[ good_matches[i].trainIdx ].pt );
}

Mat H = findHomography( obj, scene, CV_RANSAC );

答案 3 :(得分:0)

删除布局中的以下行,似乎有所有问题

android:background="@drawable/ute"