gpt4 book ai didi

java - Json 和 ViewPager。 MainActivity 无法转换为 android.app.Activity

转载 作者:行者123 更新时间:2023-12-02 13:18:35 24 4
gpt4 key购买 nike

我想要 json 和 viewpager。但我不能。错误 = MainActivity 无法转换为 android.app.Activity。我能做些什么 ?谢谢。

适配器

public class CustomAdapter extends PagerAdapter {

Context context;
String[] name;
int[] image_url;
LayoutInflater inflater;

public CustomAdapter(Context context, String[] name, int[] image_url) {
this.context = context;
this.name = name;
this.image_url = image_url;
}

@Override
public int getCount() {
return name.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {

// Declare Variables
TextView txtname;
ImageView img;

inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.image_item, container,
false);

// Locate the TextViews in image_item.xml
txtname = (TextView) itemView.findViewById(R.id.textView1);

// Capture position and set to the TextViews
txtname.setText(name[position]);

// Locate the ImageView in image_item.xml
img = (ImageView) itemView.findViewById(R.id.imageView);
// Capture position and set to the ImageView
img.setImageResource(image_url[position]);

// Add image_item.xml to ViewPager
((ViewPager) container).addView(itemView);

return itemView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove image_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);

}

}

主要 Activity Json解析和viewpager

public class MainActivity extends Fragment {
private static final String PROFILE_URL1 = "http://sunucupro.com/bt/1.txt";
JSONObject event_slider;
//private ProgressDialog pDialog;
String category_id, id;
String[] name;
int[] image_url;
ViewPager mViewPager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.activity_main, container, false);



mViewPager = (ViewPager) rootView.findViewById(R.id.pager);

new LoadSlider().execute();

return rootView;
}

class LoadSlider extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();
}

protected String doInBackground(String... args) {
// Building Parameters
String json = null;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(PROFILE_URL1);
httppost.setEntity(new UrlEncodedFormEntity(params));

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
json = EntityUtils.toString(resEntity);

Log.i("All Slider: ", json.toString());
} catch (Exception e) {
e.printStackTrace();
}

return json;
}

@Override
protected void onPostExecute(String json) {
super.onPostExecute(json);

//pDialog.dismiss();

try {
event_slider = new JSONObject(json);
final ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
JSONArray user = event_slider.getJSONArray("data");

for (int i = 0; i < user.length(); i++) {
JSONObject object = user.getJSONObject(i);
id = object.getString("id");
Log.i("id:", id);

HashMap<String, String> map = new HashMap<String, String>();
map.put("id", object.getString("id"));
map.put("name", object.getString("name"));
map.put("image_url", "http://www.iconsdb.com/icons/download/icon-sets/sketchy-pink/" + object.getString("images"));
arraylist.add(map);
}

name = new String[]{};

image_url = new int[]{};

PagerAdapter adapter = new CustomAdapter(getActivity(), name, image_url);
mViewPager.setAdapter(adapter);

} catch (Exception e) {
e.printStackTrace();
}

}
}
}

list 我将其更改为 android:name="com.sunucupro.deneme.MainActivity

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sunucupro.deneme">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name="com.sunucupro.deneme.MainActivity"
android:label="@string/app_name"

>
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

最佳答案

如果您遵循 Android 命名约定,那么该错误就很有意义。

 public class MainActivity extends Fragment {

Fragment 不是 Activity,也不能这样转换。

改回来。

public class MainActivity extends AppCompatActivity {

并了解如何 create a Fragment correctly

关于java - Json 和 ViewPager。 MainActivity 无法转换为 android.app.Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43669043/

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