我只有一个列表有问题。发生了什么事,我想拿出10个项目,但只出现一个。
我认为这是由于我的活动和片段造成的,但是您必须输入什么代码?此外,我在“ logcat”中收到一条错误消息,提示“您是否已在AndroidManifest.xml中声明此活动?”
任何帮助将不胜感激。谢谢。
* Logcat
08-30 22:31:23.981 2603-2664/? W/AASAInstall: updated apk:com.example.gonzalo.testdrawer uid:-1
08-30 22:31:28.391 2603-2664/? W/PackageManager: Code path for pkg : com.example.gonzalo.testdrawer changing from /data/app/com.example.gonzalo.testdrawer-2 to /data/app/com.example.gonzalo.testdrawer-1
08-30 22:31:28.391 2603-2664/? W/PackageManager: Resource path for pkg : com.example.gonzalo.testdrawer changing from /data/app/com.example.gonzalo.testdrawer-2 to /data/app/com.example.gonzalo.testdrawer-1
08-30 22:31:30.076 2603-2603/? E/SDAgentPackageStateReceiver: Not going to handle 'com.example.gonzalo.testdrawer'!
08-30 22:31:30.881 2603-2603/? E/SDAgentPackageStateReceiver: Not going to handle 'com.example.gonzalo.testdrawer'!
08-30 22:31:30.946 4206-4206/? E/SPPClientService: [PackageInfoChangeReceiver] [handlePkgRemovedEvent] PackageName : com.example.gonzalo.testdrawer, true, false
[PackageInfoChangeReceiver] [handlePkgRemovedEvent] Ignore Replacing case
08-30 22:31:31.426 2603-2603/? W/BackupManagerService: Removing schedule queue dupe of com.example.gonzalo.testdrawer
08-30 22:31:32.036 3057-3057/? E/Launcher.Model: onPackageChanged :com.example.gonzalo.testdrawer
08-30 22:31:32.746 4293-4293/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.gonzalo.testdrawer, PID: 4293
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gonzalo.testdrawer/com.example.gonzalo.testdrawer.FragmentActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.gonzalo.testdrawer/com.example.gonzalo.testdrawer.RecycleViewFragment}; have you declared this activity in your AndroidManifest.xml?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3133)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3243)
at android.app.ActivityThread.access$1000(ActivityThread.java:218)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1718)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6917)
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:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.gonzalo.testdrawer/com.example.gonzalo.testdrawer.RecycleViewFragment}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1837)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1534)
at android.app.Activity.startActivityForResult(Activity.java:4125)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
at android.app.Activity.startActivityForResult(Activity.java:4072)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:732)
at android.app.Activity.startActivity(Activity.java:4396)
at android.app.Activity.startActivity(Activity.java:4364)
at com.example.gonzalo.testdrawer.FragmentActivity.onCreate(FragmentActivity.java:15)
at android.app.Activity.performCreate(Activity.java:6609)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3086)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3243)
at android.app.ActivityThread.access$1000(ActivityThread.java:218)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1718)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6917)
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:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
08-30 22:31:32.851 2603-3486/? W/ActivityManager: Force finishing activity 1 com.example.gonzalo.testdrawer/.FragmentActivity
08-30 22:31:33.661 2603-2652/? W/ActivityManager: Activity pause timeout for ActivityRecord{1fa90a00 u0 com.example.gonzalo.testdrawer/.FragmentActivity t12 f}
08-30 22:31:34.691 2847-2847/? W/PackageManager: Failure retrieving resources for com.example.gonzalo.testdrawer: Resource ID #0x0
08-30 22:31:38.316 4275-4275/? W/PackageManager: Failure retrieving resources for com.example.gonzalo.testdrawer: Resource ID #0x0
08-30 22:33:00.406 4941-4941/? W/PackageManager: Failure retrieving resources for com.example.gonzalo.testdrawer: Resource ID #0x0
08-30 22:33:28.741 5447-5447/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.gonzalo.testdrawer, PID: 5447
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gonzalo.testdrawer/com.example.gonzalo.testdrawer.FragmentActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.gonzalo.testdrawer/com.example.gonzalo.testdrawer.RecycleViewFragment}; have you declared this activity in your AndroidManifest.xml?
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3133)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3243)
at android.app.ActivityThread.access$1000(ActivityThread.java:218)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1718)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6917)
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:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.gonzalo.testdrawer/com.example.gonzalo.testdrawer.RecycleViewFragment}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1837)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1534)
at android.app.Activity.startActivityForResult(Activity.java:4125)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
at android.app.Activity.startActivityForResult(Activity.java:4072)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:732)
at android.app.Activity.startActivity(Activity.java:4396)
at android.app.Activity.startActivity(Activity.java:4364)
at com.example.gonzalo.testdrawer.FragmentActivity.onCreate(FragmentActivity.java:15)
at android.app.Activity.performCreate(Activity.java:6609)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3086)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3243)
at android.app.ActivityThread.access$1000(ActivityThread.java:218)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1718)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6917)
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:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
08-30 22:33:28.746 2603-3091/? W/ActivityManager: Force finishing activity 1 com.example.gonzalo.testdrawer/.FragmentActivity
08-30 22:33:29.276 2603-2652/? W/ActivityManager: Activity pause timeout for ActivityRecord{2c42a0c9 u0 com.example.gonzalo.testdrawer/.FragmentActivity t13 f}
08-30 22:33:30.301 2847-2847/? W/PackageManager: Failure retrieving resources for com.example.gonzalo.testdrawer: Resource ID #0x0
08-30 22:35:54.861 5726-5726/? W/PackageManager: Failure retrieving resources for com.example.gonzalo.testdrawer: Resource ID #0x0
08-30 22:41:05.371 6355-6355/? W/PackageManager: Failure retrieving resources for com.example.gonzalo.testdrawer: Resource ID #0x0
08-30 22:59:26.871 11998-11998/? W/PackageManager: Failure retrieving resources for com.example.gonzalo.testdrawer: Resource ID #0x0
08-30 23:03:33.836 13685-13685/? W/PackageManager: Failure retrieving resources for com.example.gonzalo.testdrawer: Resource ID #0x0
* activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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"
tools:context=".MainActivity"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar_layout"/>
<FrameLayout
android:id="@+id/fragment_que_cambia"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="@+id/boton_flotante"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_dialog_email"
android:layout_gravity="bottom|right"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
/>
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/navigation_view"
android:theme="@style/AppTheme"
android:layout_gravity = "start"
app:itemTextColor="@color/colorAccent"
app:itemIconTint="#f0f0"
app:headerLayout="@layout/header"
app:menu="@menu/menu"/>
</android.support.v4.widget.DrawerLayout>
*这是我的活动代码
*“ AndroidManifest.xml”
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gonzalo.testdrawer">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".My_fragment_activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
* activity_note_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:id="@+id/cardview">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:weightSum="10"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="8"
android:orientation="vertical">
<TextView
android:id="@+id/texto"
android:text="Hola texto"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/titulo"
android:text="Hola titulo"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/body"
android:text="Hola body"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical">
<ImageView
android:id="@+id/xd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/yopyop" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
* NotesAdapter.java
package com.example.gonzalo.testdrawer.Adapters;
import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.gonzalo.testdrawer.POJOS.Note;
import com.example.gonzalo.testdrawer.R;
import java.util.List;
public class NotesAdapter extends
RecyclerView.Adapter<NotesAdapter.ViewHolder>{
Context context;
List<Note> noteList;
public NotesAdapter(Context context, List<Note> noteList){
this.context = context;
this.noteList = noteList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_note_item, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
//Todo_esto se aplica al TextView, y es obvio de que el TextView SOLO recibe texto(cadenas);
holder.texto.setText(String.valueOf(noteList.get(position).getId()));
holder.tituto.setText(String.valueOf(noteList.get(position).getTitle()));
holder.body.setText(String.valueOf(noteList.get(position).getBody()));
}
@Override
public int getItemCount() {
return noteList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
CardView cardView;
TextView texto, tituto, body;
ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
cardView = itemView.findViewById(R.id.cardview);
texto = itemView.findViewById(R.id.texto);
tituto = itemView.findViewById(R.id.titulo);
body = itemView.findViewById(R.id.body);
imageView = itemView.findViewById(R.id.xd);
}
}
}
* RecycleViewFragment.java
package com.example.gonzalo.testdrawer;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.gonzalo.testdrawer.Adapters.NotesAdapter;
import com.example.gonzalo.testdrawer.POJOS.Note;
import java.util.ArrayList;
import java.util.List;
public class RecycleViewFragment extends Fragment{
private RecyclerView recyclerView;
private List<Note> lista;
private NotesAdapter adapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView;
rootView = inflater.inflate(R.layout.fragment_recyclerview, container, false);
return rootView;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = view.findViewById(R.id.fragment_recyclerview);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext(),
LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
//Método donde se inicializa los datos
initializedData();
adapter = new NotesAdapter(getActivity().getApplicationContext(), lista);
recyclerView.setAdapter(adapter);
}
public void initializedData(){
lista = new ArrayList<>();
lista.add(new Note(1,"Nota 1", "Este es el teto de la nota 1"));
lista.add(new Note(2,"Nota 2", "Este es el teto de la nota 2"));
lista.add(new Note(3,"Nota 3", "Este es el teto de la nota 3"));
lista.add(new Note(4,"Nota 4", "Este es el teto de la nota 4"));
lista.add(new Note(5,"Nota 5", "Este es el teto de la nota 5"));
lista.add(new Note(6,"Nota 6", "Este es el teto de la nota 6"));
lista.add(new Note(7,"Nota 7", "Este es el teto de la nota 7"));
lista.add(new Note(8,"Nota 8", "Este es el teto de la nota 8"));
lista.add(new Note(9,"Nota 9", "Este es el teto de la nota 9"));
lista.add(new Note(10,"Nota 10", "Este es el teto de la nota 10"));
adapter.notifyDataSetChanged();
}
}
* fragment_recyclerview.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">
<android.support.v7.widget.RecyclerView
android:id="@+id/fragment_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
答案 0 :(得分:0)
解决方案:
首先,不要将您的类的名称用作FragmentActivity
...这是一个内置的类。因此,将其更改为其他内容,然后将其替换为清单。
第二,
删除
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
来自您,
<activity android:name=".FragmentActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<intent-filter>
仅对于一项活动就足够了,否则它将为不同的活动创建许多图标。
第三,
写adapter.notifyDataSetChanged()
,如下所示:
public void initializedData(){
lista = new ArrayList<>();
lista.add(new Note(1,"Nota 1", "Este es el teto de la nota 1"));
lista.add(new Note(2,"Nota 2", "Este es el teto de la nota 2"));
lista.add(new Note(3,"Nota 3", "Este es el teto de la nota 3"));
lista.add(new Note(4,"Nota 4", "Este es el teto de la nota 4"));
lista.add(new Note(5,"Nota 5", "Este es el teto de la nota 5"));
lista.add(new Note(6,"Nota 6", "Este es el teto de la nota 6"));
lista.add(new Note(7,"Nota 7", "Este es el teto de la nota 7"));
lista.add(new Note(8,"Nota 8", "Este es el teto de la nota 8"));
lista.add(new Note(9,"Nota 9", "Este es el teto de la nota 9"));
lista.add(new Note(10,"Nota 10", "Este es el teto de la nota 10"));
........ (Write here)
}
此外,您不能在MainActivity.class中使用Intent来调用Fragment类,请像这样使用:
Fragment fragment = new RecyclerViewFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_frame, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
并从MainActivity的onCreate()中删除意图
然后,更改此:
<FrameLayout
android:id="@+id/fragment_que_cambia"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="@+id/boton_flotante"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_dialog_email"
android:layout_gravity="bottom|right"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
/>
</FrameLayout>
收件人:
<FrameLayout
android:id="@+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
最后,
在您的onCreate()中:
setContentView(R.layout.activity_main);
就这样
希望有帮助。
答案 1 :(得分:0)
请检查以下代码段,您犯了一些小错误。 如果不清楚,请告诉我。
RecycleViewFragment.java 进行了如下较小更改。
package com.example.gonzalo.testdrawer;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.gonzalo.testdrawer.Adapters.NotesAdapter;
import com.example.gonzalo.testdrawer.POJOS.Note;
import java.util.ArrayList;
import java.util.List;
public class RecycleViewFragment extends Fragment{
private RecyclerView recyclerView;
private List<Note> lista;
private NotesAdapter adapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView;
rootView = inflater.inflate(R.layout.fragment_recyclerview, container, false);
return rootView;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = view.findViewById(R.id.fragment_recyclerview);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext(),
LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
//Método donde se inicializa los datos
adapter = new NotesAdapter(getActivity().getApplicationContext(), lista);
recyclerView.setAdapter(adapter);
//---------------- Change is here ------------------------------
initializedData(); // call this function after initialization and assignment of adapter.
}
public void initializedData(){
lista = new ArrayList<>();
lista.add(new Note(1,"Nota 1", "Este es el teto de la nota 1"));
lista.add(new Note(2,"Nota 2", "Este es el teto de la nota 2"));
lista.add(new Note(3,"Nota 3", "Este es el teto de la nota 3"));
lista.add(new Note(4,"Nota 4", "Este es el teto de la nota 4"));
lista.add(new Note(5,"Nota 5", "Este es el teto de la nota 5"));
lista.add(new Note(6,"Nota 6", "Este es el teto de la nota 6"));
lista.add(new Note(7,"Nota 7", "Este es el teto de la nota 7"));
lista.add(new Note(8,"Nota 8", "Este es el teto de la nota 8"));
lista.add(new Note(9,"Nota 9", "Este es el teto de la nota 9"));
lista.add(new Note(10,"Nota 10", "Este es el teto de la nota 10"));
adapter.notifyDataSetChanged();
}
}