使用 AsyncTaskLoader 时出错
这是我的 FragmentListArraySupport.java
package com.test.methods;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FragmentListArraySupport extends FragmentActivity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the list fragment and add it as our sole content.
if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
final ArrayListFragment list = new ArrayListFragment();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();
}
}
public static class ArrayListFragment extends ListFragment {
@Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, Shakespeare.TITLES));
}
@Override
public void onListItemClick(final ListView l, final View v, final int position, final long id) {
Log.i("FragmentList", "Item clicked: " + id);
}
}
}
这是我的加载程序:
package com.test.methods;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.support.v4.content.AsyncTaskLoader;
public class AsyncLoader extends AsyncTaskLoader<String[]> {
public AsyncLoader(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public String[] loadInBackground() {
// TODO Auto-generated method stub
String[] S = {"hello","hi","bye"};
return S;
}
}
我的清单是这样的:
<activity android:name=".app.FragmentListArraySupport"
android:label="@string/fragment_list_array_support">
</activity>
错误:
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.crumbin.main/com.test.methods.FragmentListArraySupport}; have you declared this activity in your AndroidManifest.xml?
可能是什么错误?
这是我处理片段并使用 AsyncTaskLoaders 的最快时间,它没有太多文档或示例。我的代码所做或应该做的只是打印字符串列表。我可以让他们改进。
我的代码有问题吗?在哪里?
This is my FragmentListArraySupport.java
package com.test.methods;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FragmentListArraySupport extends FragmentActivity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the list fragment and add it as our sole content.
if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
final ArrayListFragment list = new ArrayListFragment();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();
}
}
public static class ArrayListFragment extends ListFragment {
@Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, Shakespeare.TITLES));
}
@Override
public void onListItemClick(final ListView l, final View v, final int position, final long id) {
Log.i("FragmentList", "Item clicked: " + id);
}
}
}
This is my loader:
package com.test.methods;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.support.v4.content.AsyncTaskLoader;
public class AsyncLoader extends AsyncTaskLoader<String[]> {
public AsyncLoader(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public String[] loadInBackground() {
// TODO Auto-generated method stub
String[] S = {"hello","hi","bye"};
return S;
}
}
My manifest is this:
<activity android:name=".app.FragmentListArraySupport"
android:label="@string/fragment_list_array_support">
</activity>
Error:
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.crumbin.main/com.test.methods.FragmentListArraySupport}; have you declared this activity in your AndroidManifest.xml?
What might be the error?
This is the fast time I'm dealing with fragments and also using AsyncTaskLoaders which hasn't got much documentation or examples. What my code does or should do is just print a list of strings. I can them improve on it.
Is there something wrong with my code? Where?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的清单是完整的。
您的清单应该包含该活动:
或者
对于第二种工作方式,请转到 Strings.xml 文件并添加 Activity_name =“活动的任何标签。”
仅此而已。
Your manifest is completely.
Your manifest should have this to include the activity:
Or
For the second way to work, go to the Strings.xml file and add Activity_name = "Any label for the activity."
That is all.
片段必须包含在活动中(您可以在 XML 中或动态地执行此操作)。在清单中,您必须指定可能包含的活动,而不是片段。
A fragment must be contained in an activity (you can do this in XML or dynamically). In the manifest you have to specify the activities, not the fragments, which may be contained.
您的清单文件中的代码是错误的。应该有一个活动而不是一个片段。这就是为什么你会得到 CAST 异常。
请查看示例:
http://developer.android.com/resources/samples/Support4Demos/index.html
您可以使用 Android SDK Manager 下载它们。
我在下面附上一个简单的示例:
并在您的清单文件中:
Your code in the manifest file is wrong. There should be an activity instead of a fragment. That is why you get that CAST exception.
Please check out the samples:
http://developer.android.com/resources/samples/Support4Demos/index.html
You can download them with the Android SDK Manager.
I attach a simple example below:
and in you manifest file: