gpt4 book ai didi

javascript - androidx.appcompat.widget.LinearLayoutCompat 无法转换为 android.widget.LinearLayout

转载 作者:行者123 更新时间:2023-12-02 20:49:13 24 4
gpt4 key购买 nike

我有一个 Android 应用程序。这个应用程序有十个 Activity ,其中第一个 Activity 是运行的启动屏幕,在一两秒后,应用程序关闭并且不会进入第二个 Activity 我得到的错误是:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 4538
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.IntroPage}: java.lang.ClassCastException: androidx.appcompat.widget.LinearLayoutCompat cannot be cast to android.widget.LinearLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.ClassCastException: androidx.appcompat.widget.LinearLayoutCompat cannot be cast to android.widget.LinearLayout
at com.example.myapplication.IntroPage.onCreate(IntroPage.java:45)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

这是我的 xml 启动屏幕代码:

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".SplashScreen"
android:background="@color/White">



<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/img_gif"
android:layout_width="match_parent"
android:layout_height="289dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="38dp"
android:src="@drawable/ic_profile"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.296" />


<androidx.core.widget.ContentLoadingProgressBar
android:id="@+id/progress"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.005"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<com.github.ybq.android.spinkit.SpinKitView
android:id="@+id/spin_kit"
style="@style/SpinKitView.WanderingCubes"
android:layout_width="60dp"
android:layout_height="49dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="36dp"
android:layout_marginRight="8dp"
app:SpinKit_Color="@color/orange"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/img_gif"
app:layout_constraintVertical_bias="0.254" />

<TextView
android:id="@+id/textView2"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="8dp"
android:layout_marginBottom="2dp"
android:gravity="center"
android:text="@string/powerd_by_maede"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/spin_kit"
app:layout_constraintVertical_bias="0.48"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是我的 sec Activity xml:

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9bfa5e">

<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="0dp"
android:layout_height="435dp"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/layoutDots"
app:layout_constraintRight_toRightOf="@+id/layoutDots"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="8dp"
app:layout_constraintLeft_toRightOf="@+id/layoutDots" />

<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/layoutDots"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:orientation="horizontal"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent" />

<View
android:layout_width="0dp"
android:layout_height="1dp"
android:alpha=".5"
android:background="#222"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/btn_next"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@+id/btn_next" />

<Button
android:id="@+id/btn_next"
android:layout_width="88dp"
android:layout_height="50dp"
android:background="@drawable/chevron_right"
android:textColor="#222"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintLeft_toRightOf="@+id/btn_skip"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1" />

<Button
android:id="@+id/btn_skip"
android:layout_width="88dp"
android:layout_height="48dp"
android:layout_marginBottom="1dp"
android:background="@null"
android:text="رد کردن"
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/layoutDots"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1" />

</androidx.constraintlayout.widget.ConstraintLayout>

这就是我的闪屏 Activity :

   package com.example.myapplication;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import androidx.appcompat.app.AppCompatActivity;
import android.view.WindowManager;
import android.widget.ProgressBar;
import com.github.ybq.android.spinkit.sprite.Sprite;
import com.github.ybq.android.spinkit.style.DoubleBounce;
import java.util.Calendar;

public class SplashScreen extends AppCompatActivity {

SharedPreferences sharedP;
boolean show_intropage=true;
Calendar taqwim = Calendar.getInstance();
int today,date_mod;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);


getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
//----progress---------------
ProgressBar progressBar = findViewById(R.id.progress);
Sprite doubleBounce = new DoubleBounce();
progressBar.setIndeterminateDrawable(doubleBounce);

today = taqwim.get(Calendar.DAY_OF_YEAR);

sharedP = getSharedPreferences(Items.SETTINGS, Context.MODE_PRIVATE);
show_intropage = sharedP.getBoolean(Items.INTROPAGE,true);

sharedP.edit().putInt(Items.TODAY, today).apply();




if (show_intropage){

new Handler().postDelayed(new Runnable() {
@Override
public void run() {

Intent Myintent = new Intent(SplashScreen.this,IntroPage.class);
startActivity(Myintent);
finish();

}
},4000);

date_mod = taqwim.get(Calendar.DAY_OF_YEAR);
// Toast.makeText(this, day+"", Toast.LENGTH_SHORT).show();
sharedP.edit().putInt(Items.DATE_MODIFIED, date_mod).apply();


}else {

new Handler().postDelayed(new Runnable() {
@Override
public void run() {

Intent Myintent = new Intent(SplashScreen.this,MainActivity.class);
startActivity(Myintent);
finish();

}
},4000);
}
}
}

这是我的第二次 Activity :

    package com.example.myapplication;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ToxicBakery.viewpager.transforms.RotateUpTransformer;


public class IntroPage extends AppCompatActivity {

ViewPager MyviewPager;
Button btnSkip, btnNext;
private LinearLayout dotsLayout;
private TextView[] dots;

SharedPreferences sharedP;

int [] layout = {R.layout.slide_01,R.layout.slide_02,R.layout.slide_04};

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


getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

MyviewPager = (ViewPager) findViewById(R.id.viewpager);

btnSkip = (Button) findViewById(R.id.btn_skip);
btnNext = (Button) findViewById(R.id.btn_next);
dotsLayout = (LinearLayout) findViewById(R.id.layoutDots);
MyviewPager.setAdapter(new ViewP());

MyviewPager.setPageTransformer(true, new RotateUpTransformer());
sharedP = getSharedPreferences(Items.SETTINGS, Context.MODE_PRIVATE);

addBottomDots(0);


btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

int Tedad_items = MyviewPager.getCurrentItem()+1;

if (Tedad_items < layout.length) {

MyviewPager.setCurrentItem(MyviewPager.getCurrentItem()+1);

} else {

startActivity(new Intent(IntroPage.this,MainActivity.class));
finish();

}

}
});

btnSkip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(IntroPage.this,MainActivity.class));
finish();

}
});

MyviewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {

addBottomDots(position);

if (position == layout.length - 1) {
// btnNext.setText("Start");
btnNext.setBackgroundResource(R.drawable.check);
btnSkip.setVisibility(View.GONE);
} else {
// btnNext.setText("Next");
btnNext.setBackgroundResource(R.drawable.chevron_right);
btnSkip.setVisibility(View.VISIBLE);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});

}

public class ViewP extends PagerAdapter {

@Override
public Object instantiateItem(ViewGroup container, int position) {

View view= LayoutInflater.from(IntroPage.this).inflate(layout[position],container,false);
container.addView(view);

return view;
}

@Override
public int getCount() {
return layout.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {

// View view = (View)object;
container.removeView((View) object);


}
}
private void addBottomDots(int currentPage) {
dots = new TextView[layout.length];

int[] colorsActive = getResources().getIntArray(R.array.array_dot_active);
int[] colorsInactive = getResources().getIntArray(R.array.array_dot_inactive);

dotsLayout.removeAllViews();
for (int i = 0; i < dots.length; i++) {
dots[i] = new TextView(this);
dots[i].setText(Html.fromHtml("&#8226;"));
dots[i].setTextSize(35);
dots[i].setTextColor(colorsInactive[0]);
dotsLayout.addView(dots[i]);
}

if (dots.length > 0)
dots[currentPage].setTextColor(colorsActive[0]);
}
}

最佳答案

问题在这里:

dotsLayout = (LinearLayout) findViewById(R.id.layoutDots);

在您的布局中,您使用的是LinearLayoutCompat
使用:

LinearLayoutCompat dotsLayout;
dotsLayout = findViewById(R.id.layoutDots);

关于javascript - androidx.appcompat.widget.LinearLayoutCompat 无法转换为 android.widget.LinearLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61655216/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com