gpt4 book ai didi

android - 引用特定 fragment 时 SectionsPagerAdapter 类中的错误

转载 作者:行者123 更新时间:2023-11-30 03:06:58 26 4
gpt4 key购买 nike

我正在尝试使用 Switch 方法引用特定 fragment ,当引用特定 fragment 时,IDE (Eclipse) 会抛出错误。

具体错误是:类型不匹配:无法从 GuidelineFragment 转换为 Fragment

我的第一个怀疑是我将 GuidelineFragment 的 Activity 扩展到 ListActivity,但是当我扩展到 ListFragment 时抛出更多错误并恢复到 ListActivity 扩展。

我组织了导入并清理了项目,但没有成功。

很遗憾,以下 StackOverflow 问题并未具体回答我的问题:

Android Fragments for Tabbed Menu

Problems with the tabs and the fragment

我做错了什么?

以下是我项目中的文件

SectionsPagerAdapter 类

package com.example.perinatologiev2;

import java.util.Locale;

import com.example.perinatologiev2.R;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class SectionsPagerAdapter extends FragmentPagerAdapter {

protected Context mContext;

/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/

public SectionsPagerAdapter(Context context,FragmentManager fm) {
super(fm);
mContext = context;
}

@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page..
switch(position) {
case 0:
return new GuidelineFragment(); //error here
case 1:
return new CentraFragment(); //error here
case 2:
return new UzitecneFragment(); //error here
}

return null;

}

@Override
public int getCount() {
// Showing 3 total pages.
return 3;
}

@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return mContext.getString(R.string.title_section1).toUpperCase(l);
case 1:
return mContext.getString(R.string.title_section2).toUpperCase(l);
case 2:
return mContext.getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}

这是 switch 语句引用的 fragment :

指南 fragment

package com.example.perinatologiev2;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import com.example.perinatologiev2.R;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class GuidelineFragment extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guideline_fragment);
try
{
JSONArray jsonArray = parsePostupyJSON();
ArrayList<String> fields = new ArrayList<String>();
for (int index = 0; index < jsonArray.length(); index++) {
//Set both values into the listview
JSONObject jsonObject = jsonArray.getJSONObject(index);
fields.add(jsonObject.getString("title"));
}

setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fields));
} catch (FileNotFoundException e) {
Log.e("jsonFile", "file not found");
} catch (IOException e) {
Log.e("jsonFile", "ioerror");
} catch (JSONException e) {
e.printStackTrace();
}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.guideline, menu);
return true;
}

private JSONArray parsePostupyJSON() throws IOException, JSONException {
//Load File
BufferedReader jsonReader = new BufferedReader(new InputStreamReader(this.getResources().openRawResource(R.raw.guidelines)));

StringBuilder jsonBuilder = new StringBuilder();
for (String line = null; (line = jsonReader.readLine()) != null;) {
jsonBuilder.append(line).append("\n");
}

//Parse Json
JSONTokener tokener = new JSONTokener(jsonBuilder.toString());
JSONArray jsonArray = new JSONArray(tokener);
return jsonArray;
}


//The following is the method that I need to modify to listen to the tap and direct to web view

protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);

try {
JSONArray jsonArray = parsePostupyJSON();
JSONObject jsonPost = jsonArray.getJSONObject(position);
String guidelineUrl = jsonPost.getString("guidelinepath");
Intent intent = new Intent(this, GuidelineWebActivity.class);
intent.setData(Uri.parse(guidelineUrl));
startActivity(intent);
}
catch (JSONException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}

}

最后但同样重要的是......主要 Activity

package com.example.perinatologiev2;

import com.example.perinatologiev2.R;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {

public static final String TAG = MainActivity.class.getSimpleName();

/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/

SectionsPagerAdapter mSectionsPagerAdapter;

/**
* The {@link ViewPager} that will host the section contents.
*/

ViewPager mViewPager;

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

// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(this,
getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);

// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});

// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}


}

最佳答案

如果您想在 ViewPager 中使用 GuidelineFragment(和其他类),它们需要扩展 Fragment 而不是 Activity。这两个有不同的方法,你应该重写,这就是为什么你在更改父类(super class)后得到这么多错误的原因,例如在处理 Fragment 而不是 ActivityonCreate 时,您通常会使用 onCreateView

关于android - 引用特定 fragment 时 SectionsPagerAdapter 类中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21646019/

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