Android Studio: unabule to start activity ComponentInfo: jav



我是安卓工作室的新手。我目前正在为我的学校项目开发一个应用程序,我遇到了一个问题,我试图寻找解决方案,但无济于事。这是我的错误。请帮帮我谢谢!!

07-02 16:24:05.882 27824-27824/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{sg.edu.tp.moviereviewflexiv21/sg.edu.tp.moviereviewflexiv21.activities.HomePagePrototype}: java.lang.IllegalStateException: RecyclerView has no LayoutManager android.support.v7.widget.RecyclerView{22d56d58 VFED.... ......I. 0,0-0,0 #7f08009c app:id/recyclerview_trending}, adapter:null, layout:null, context:sg.edu.tp.moviereviewflexiv21.activities.HomePagePrototype@3e90d3d1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.IllegalStateException: RecyclerView has no LayoutManager android.support.v7.widget.RecyclerView{22d56d58 VFED.... ......I. 0,0-0,0 #7f08009c app:id/recyclerview_trending}, adapter:null, layout:null, context:sg.edu.tp.moviereviewflexiv21.activities.HomePagePrototype@3e90d3d1
at android.support.v7.widget.RecyclerView.generateLayoutParams(RecyclerView.java:4192)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:808)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at sg.edu.tp.moviereviewflexiv21.activities.HomePagePrototype.onCreate(HomePagePrototype.java:34)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 

这是我的回收器视图适配器2.java:

public class RecyclerVIewAdapter2 extends RecyclerView.Adapter<RecyclerVIewAdapter2.ViewHolder> {
private static final String TAG = "RecyclerVIewAdapter2";
//vars
private ArrayList<String> mNames = new ArrayList<>();
private ArrayList<String> mImageUrls = new ArrayList<>();
private Context mContext;
public RecyclerVIewAdapter2(Context mContext, ArrayList<String> mNames, ArrayList<String> mImageUrls) {
this.mNames = mNames;
this.mImageUrls = mImageUrls;
this.mContext = mContext;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
Glide.with(mContext)
.asBitmap()
.load(mImageUrls.get(position))
.into(holder.image);
holder.name.setText(mNames.get(position));
holder.image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: Clicked on an image: " + mNames.get(position));
Toast.makeText(mContext, mNames.get(position), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return mImageUrls.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView image;
TextView name;
public ViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.imageofmovie);
name = itemView.findViewById(R.id.nameofmovie);
}
}

}

这是我的Android清单.xml:

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

<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/flexireview"
android:label="@string/app_name"
android:roundIcon="@mipmap/flexireview_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activities.Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.SignIn" />
<activity android:name=".activities.NavigationDrawer" />
<activity
android:name=".activities.TestingBurgerMenu"
android:label="@string/title_activity_home_page"
android:theme="@style/AppTheme" />
<activity
android:name=".HomePageBottomNav"
android:label="@string/title_activity_home_page_bottom_nav" />
<activity android:name=".BottomBarNavTesting" />
<activity
android:name=".activities.HomePagePrototype"
android:label="@string/title_activity_home_page_prototype"
android:theme="@style/AppTheme"></activity>
</application>
</manifest>

这是我的风格.xml:

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="coordinatorLayoutStyle">@style/Widget.Design.CoordinatorLayout</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

这是我的整个HomePagePrototype.java文件,通常称为MainActivity.java:

public class HomePagePrototype extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
//vars
private ArrayList<String> mNames = new ArrayList<>();
private ArrayList<String> mImageUrls = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page_prototype);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
getImages();
}
private void getImages(){
mImageUrls.add("https://image.tmdb.org/t/p/original/c9XxwwhPHdaImA2f1WEfEsbhaFB.jpg");
mNames.add("Jurassic World:Fallen Kingdom");
mImageUrls.add("https://image.tmdb.org/t/p/original/x1txcDXkcM65gl7w20PwYSxAYah.jpg");
mNames.add("Incredibles 2");
mImageUrls.add("https://image.tmdb.org/t/p/original/rzRwTcFvttcN1ZpX2xv4j3tSdJu.jpg");
mNames.add("Thor: Ragnarok");
mImageUrls.add("https://image.tmdb.org/t/p/original/3gIO6mCd4Q4PF1tuwcyI3sjFrtI.jpg");
mNames.add("Rampage (2018)");
mImageUrls.add("https://image.tmdb.org/t/p/original/7WsyChQLEftFiDOVTGkv3hFpyyt.jpg");
mNames.add("Avengers: Infinity War");
mImageUrls.add("https://image.tmdb.org/t/p/original/jjPJ4s3DWZZvI4vw8Xfi4Vqa1Q8.jpg");
mNames.add("Fifty Shades Freed");
mImageUrls.add("https://image.tmdb.org/t/p/original/jjBgi2r5cRt36xF6iNUEhzscEcb.jpg");
mNames.add("Jurassic World (2015)");
mImageUrls.add("https://image.tmdb.org/t/p/original/v5HlmJK9bdeHxN2QhaFP1ivjX3U.jpg");
mNames.add("Pacific Rim: Uprising (2018)");
mImageUrls.add("https://image.tmdb.org/t/p/original/uxzzxijgPIY7slzFvMotPv8wjKA.jpg");
mNames.add("Black Panther (2018)");
mImageUrls.add("https://image.tmdb.org/t/p/original/pU1ULUq8D3iRxl1fdX2lZIzdHuI.jpg");
mNames.add("Ready Player One (2018)");
}
private void initRecyclerView(){
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView recyclerView = findViewById(R.id.recyclerview_trending);
recyclerView.setLayoutManager(layoutManager);
RecyclerVIewAdapter2 adapter = new RecyclerVIewAdapter2(this, mNames, mImageUrls);
recyclerView.setAdapter(adapter);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home_page_prototype, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}

我在recyclerview上关注了这个视频:https://www.youtube.com/watch?v=94rCjYxvzEE

这是一个模糊的问题,但它确实在错误中指出"RecyclerView没有LayoutManager"。你可以谷歌一下,得到很多支持。下面是您可能尝试实现的内容的示例教程。

示例教程

我修复了它。显然,问题出在我的XML文件中,我使recyclerview成为我的XML中的按钮和其他文本视图的父级。为了修复它,我从回收器视图部分删除了所有孩子。

最新更新