Java 如何将FragmentActivity实现为片段?
我是Android应用程序的新手,现在正在制作我的第一个应用程序,我遇到了一些问题 根据本教程,我已成功制作了一个导航抽屉: 我发现这个例子正是我所需要的——解析XML并将其显示为一个列表,其中包含打开更详细视图的图像: 我在将最后一个示例实现到我的应用程序(包括第一个链接)中时遇到了巨大的问题,因为示例使用了FragmentActivity,而我的应用程序从我的MainActivity创建了新的片段(我知道FragmentActivity和片段是不同的) MainActivity如何创建新片段:Java 如何将FragmentActivity实现为片段?,java,android,view,fragment,android-fragmentactivity,Java,Android,View,Fragment,Android Fragmentactivity,我是Android应用程序的新手,现在正在制作我的第一个应用程序,我遇到了一些问题 根据本教程,我已成功制作了一个导航抽屉: 我发现这个例子正是我所需要的——解析XML并将其显示为一个列表,其中包含打开更详细视图的图像: 我在将最后一个示例实现到我的应用程序(包括第一个链接)中时遇到了巨大的问题,因为示例使用了FragmentActivity,而我的应用程序从我的MainActivity创建了新的片段(我知道FragmentActivity和片段是不同的) MainActivity如何创建新
private void displayView(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new NewsFragment();
break; }
public class SplashActivity extends Activity {
private String RSSFEEDURL = "http://www.nordichardware.se/feed/rss.html";
RSSFeed feed;
String fileName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
fileName = "TDRSSFeed.td";
File feedFile = getBaseContext().getFileStreamPath(fileName);
ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() == null) {
// No connectivity. Check if feed File exists
if (!feedFile.exists()) {
// No connectivity & Feed file doesn't exist: Show alert to exit
// & check for connectivity
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(
"Unable to reach server, \nPlease check your connectivity.")
.setTitle("TD RSS Reader")
.setCancelable(false)
.setPositiveButton("Exit",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int id) {
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
} else {
// No connectivty and file exists: Read feed from the File
Toast toast = Toast.makeText(this,
"No connectivity! Reading last update...",
Toast.LENGTH_LONG);
toast.show();
feed = ReadFeed(fileName);
startLisActivity(feed);
}
} else {
// Connected - Start parsing
new AsyncLoadXMLFeed().execute();
}
}
private void startLisActivity(RSSFeed feed) {
Bundle bundle = new Bundle();
bundle.putSerializable("feed", feed);
// launch List activity
Intent intent = new Intent(SplashActivity.this, ListActivity.class);
intent.putExtras(bundle);
startActivity(intent);
// kill this activity
finish();
}
private class AsyncLoadXMLFeed extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
// Obtain feed
DOMParser myParser = new DOMParser();
feed = myParser.parseXml(RSSFEEDURL);
if (feed != null && feed.getItemCount() > 0)
WriteFeed(feed);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
startLisActivity(feed);
}
}
// Method to write the feed to the File
private void WriteFeed(RSSFeed data) {
FileOutputStream fOut = null;
ObjectOutputStream osw = null;
try {
fOut = openFileOutput(fileName, MODE_PRIVATE);
osw = new ObjectOutputStream(fOut);
osw.writeObject(data);
osw.flush();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Method to read the feed from the File
private RSSFeed ReadFeed(String fName) {
FileInputStream fIn = null;
ObjectInputStream isr = null;
RSSFeed _feed = null;
File feedFile = getBaseContext().getFileStreamPath(fileName);
if (!feedFile.exists())
return null;
try {
fIn = openFileInput(fName);
isr = new ObjectInputStream(fIn);
_feed = (RSSFeed) isr.readObject();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
fIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return _feed;
}
}
我需要制作一个由SplashActivity内部组成的片段
我需要做什么才能将SplashActivity实现到我的MainActivity中(从而创建一个新的片段)?我需要将FragmentActivity转换为片段,还是需要找到一个全新的解决方案
如果你需要并且想自己尝试一下,所有的东西都可以从上面的链接获得。由于我是一个完全的初学者,我真的希望我能使用上面的例子,因为它适合我的应用程序完美
splash活动:
private void displayView(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new NewsFragment();
break; }
public class SplashActivity extends Activity {
private String RSSFEEDURL = "http://www.nordichardware.se/feed/rss.html";
RSSFeed feed;
String fileName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
fileName = "TDRSSFeed.td";
File feedFile = getBaseContext().getFileStreamPath(fileName);
ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() == null) {
// No connectivity. Check if feed File exists
if (!feedFile.exists()) {
// No connectivity & Feed file doesn't exist: Show alert to exit
// & check for connectivity
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(
"Unable to reach server, \nPlease check your connectivity.")
.setTitle("TD RSS Reader")
.setCancelable(false)
.setPositiveButton("Exit",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int id) {
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
} else {
// No connectivty and file exists: Read feed from the File
Toast toast = Toast.makeText(this,
"No connectivity! Reading last update...",
Toast.LENGTH_LONG);
toast.show();
feed = ReadFeed(fileName);
startLisActivity(feed);
}
} else {
// Connected - Start parsing
new AsyncLoadXMLFeed().execute();
}
}
private void startLisActivity(RSSFeed feed) {
Bundle bundle = new Bundle();
bundle.putSerializable("feed", feed);
// launch List activity
Intent intent = new Intent(SplashActivity.this, ListActivity.class);
intent.putExtras(bundle);
startActivity(intent);
// kill this activity
finish();
}
private class AsyncLoadXMLFeed extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
// Obtain feed
DOMParser myParser = new DOMParser();
feed = myParser.parseXml(RSSFEEDURL);
if (feed != null && feed.getItemCount() > 0)
WriteFeed(feed);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
startLisActivity(feed);
}
}
// Method to write the feed to the File
private void WriteFeed(RSSFeed data) {
FileOutputStream fOut = null;
ObjectOutputStream osw = null;
try {
fOut = openFileOutput(fileName, MODE_PRIVATE);
osw = new ObjectOutputStream(fOut);
osw.writeObject(data);
osw.flush();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Method to read the feed from the File
private RSSFeed ReadFeed(String fName) {
FileInputStream fIn = null;
ObjectInputStream isr = null;
RSSFeed _feed = null;
File feedFile = getBaseContext().getFileStreamPath(fileName);
if (!feedFile.exists())
return null;
try {
fIn = openFileInput(fName);
isr = new ObjectInputStream(fIn);
_feed = (RSSFeed) isr.readObject();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
fIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return _feed;
}
}
公共类SplashActivity扩展活动{
专用字符串RSSFEEDURL=”http://www.nordichardware.se/feed/rss.html";
RSSFeed饲料;
字符串文件名;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
fileName=“TDRSSFeed.td”;
File feedFile=getBaseContext().getFileStreamPath(文件名);
ConnectivityManager conMgr=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_服务);
if(conMgr.getActiveNetworkInfo()==null){
//无连接。请检查源文件是否存在
如果(!feedFile.exists()){
//无连接源文件不存在:显示要退出的警报
//检查连接(&C)
AlertDialog.Builder=新建AlertDialog.Builder(此);
builder.setMessage(
“无法访问服务器,\n请检查您的连接。”)
.setTitle(“TD RSS阅读器”)
.setCancelable(错误)
.setPositiveButton(“退出”,
新建DialogInterface.OnClickListener(){
@凌驾
公共void onClick(对话框接口对话框,
int id){
完成();
}
});
AlertDialog alert=builder.create();
alert.show();
}否则{
//不存在连接和文件:从文件读取提要
Toast Toast=Toast.makeText(此,
“无连接!正在读取上次更新…”,
吐司长度(长);
toast.show();
feed=ReadFeed(文件名);
惊吓活性(feed);
}
}否则{
//连接启动解析
新建AsyncLoadXMLFeed().execute();
}
}
私人无效输入活动(RSSFeed馈送){
Bundle=新Bundle();
bundle.putSerializable(“提要”,提要);
//启动列表活动
意向意向=新意向(SplashActivity.this、ListActivity.class);
意向。额外支出(捆绑);
星触觉(意向);
//终止此活动
完成();
}
私有类AsyncLoadXMLFeed扩展了AsyncTask{
@凌驾
受保护的Void doInBackground(Void…参数){
//获取饲料
DOMParser myParser=新的DOMParser();
feed=myParser.parseXml(RSSFEEDURL);
if(feed!=null&&feed.getItemCount()>0)
写进(进);
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
惊吓活性(feed);
}
}
//方法将提要写入文件
专用void WriteFeed(RSSFeed数据){
FileOutputStream fOut=null;
ObjectOutputStream osw=null;
试一试{
fOut=openFileOutput(文件名,模式\私有);
osw=新对象输出流(fOut);
osw.writeObject(数据);
osw.flush();
}
捕获(例外e){
e、 printStackTrace();
}
最后{
试一试{
fOut.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
//方法从文件中读取提要
专用RSSFeed ReadFeed(字符串fName){
FileInputStream fIn=null;
ObjectInputStream isr=null;
RSSFeed _feed=null;
File feedFile=getBaseContext().getFileStreamPath(文件名);
如果(!feedFile.exists())
返回null;
试一试{
fIn=openFileInput(fName);
isr=新目标输入流(fIn);
_提要=(RSSFeed)isr.readObject();
}
捕获(例外e){
e、 printStackTrace();
}
最后{
试一试{
fIn.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回(u)馈送;;
}
}
如果要创建片段,必须扩展片段类
public class SplashActivity extends Fragment{
//your fragment code.
}
//Also you need to actually use the fragment to do this you can create an intent and start the intent or you can try
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
将碎片活动转换为碎片有点像将鸡转换为蛋。我建议你在做这样的特技表演之前先阅读第一篇。谢谢你的投入,但我知道,这更像是一个有经验的问题。我的问题是如何在我的应用程序中实现这个示例。有什么建议吗?