我想用FragmentActivity实现Tab栏列表。我的目标是API 14(4.0)。我的应用程序工作正常,但标签栏列表没有出现...我不知道我做错了什么...感谢您的帮助!
MainActivity.java:
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.annotation.TargetApi;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Build;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import butterknife.ButterKnife;
import butterknife.InjectView;
import static android.location.Location.convert;
public class MainActivity extends ActionBarActivity implements
ActionBar.TabListener {
public static final String TAG = MainActivity.class.getSimpleName();
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link android.support.v4.view.ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
private CurrentReportString mCurrentReportString;
@InjectView(R.id.jsonText) TextView mJsonText;
@InjectView(R.id.imageReport) ImageView mImageReport;
@InjectView(R.id.imageReport2) ImageView mImageReport2;
@InjectView(R.id.imageReport3) ImageView mImageReport3;
@InjectView(R.id.adminText) TextView mAdminText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
mAdminText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
}
});
String urlWebSite = "http://www.bundoransurfco.com/surf-report/surf-report/?json=1";
if (isNetworkAvailable()) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(urlWebSite)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
try {
String jsonData = response.body().string();
if (response.isSuccessful()) {
mCurrentReportString = getCurrentDetails(jsonData);
int numberOfPhotos = mCurrentReportString.getNumberOfPhotos();
if (numberOfPhotos == 1){
new DownloadImageTask(mImageReport)
.execute(mCurrentReportString.getUrlImages());
}
if (numberOfPhotos == 2){
new DownloadImageTask(mImageReport)
.execute(mCurrentReportString.getUrlImages());
new DownloadImageTask(mImageReport2)
.execute(mCurrentReportString.getUrlImages2());
}
if (numberOfPhotos == 3){
new DownloadImageTask(mImageReport)
.execute(mCurrentReportString.getUrlImages());
new DownloadImageTask(mImageReport2)
.execute(mCurrentReportString.getUrlImages2());
new DownloadImageTask(mImageReport3)
.execute(mCurrentReportString.getUrlImages3());
}
if (numberOfPhotos > 3){
new DownloadImageTask(mImageReport)
.execute(mCurrentReportString.getUrlImages());
new DownloadImageTask(mImageReport2)
.execute(mCurrentReportString.getUrlImages2());
new DownloadImageTask(mImageReport3)
.execute(mCurrentReportString.getUrlImages3());
}
// Write in the main Thread.
runOnUiThread(new Runnable() {
@Override
public void run() {
updateReport();
}
});
} else {
alertUserAboutError();
}
} catch (IOException e) {
Log.e(TAG, "Exception caught: ", e);
}
catch (JSONException e){
Log.e(TAG, "Exception caught: ", e);
}
}
});
}
else {
Toast.makeText(this, getString(R.string.network_unavailable_message), Toast.LENGTH_LONG).show();
}
Log.d(TAG, "Main UI code is running!");
}
@Override
protected void onResume() {
super.onResume();
initUI();
}
private void updateReport() {
mJsonText.setText(mCurrentReportString.getJsonContent());
if (mCurrentReportString.imageBackground == Boolean.FALSE)
{
Log.i(TAG, "No Background1");
mImageReport.setVisibility(View.INVISIBLE);
}
if (mCurrentReportString.imageBackground2 == Boolean.FALSE)
{
Log.i(TAG, "No Background2");
mImageReport2.setVisibility(View.INVISIBLE);
}
if (mCurrentReportString.imageBackground3 == Boolean.FALSE)
{
Log.i(TAG, "No Background3");
mImageReport3.setVisibility(View.INVISIBLE);
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private CurrentReportString getCurrentDetails(String jsonData) throws JSONException {
JSONObject report = new JSONObject(jsonData);
JSONObject postJson = report.getJSONObject("post");
CurrentReportString currentReportString = new CurrentReportString();
String contentString = postJson.getString("content");
//Report String
String contentWithoutWebcam = contentString.replace("CLICK HERE FOR LIVE PEAK WEBCAM", "");
int lastCharacter = contentWithoutWebcam.indexOf("#gallery-1");
String contentFirstPart = contentWithoutWebcam.substring(0, lastCharacter);
String jsonText = stripHtml(contentFirstPart);
currentReportString.setJsonContent(jsonText);
//Images
Document doc = Jsoup.parse(contentString);
Elements relLinks = doc.select("a[rel]");
//Elements relLinks = doc.select("a[rel=prettyPhoto[gallery-113]]");//
System.out.println("number of `rel`: "+relLinks.size());
currentReportString.setNumberOfPhotos(relLinks.size());
ArrayList<String> urls=new ArrayList<String>();
for (Element el : relLinks){
// Log.i(TAG, el.attr("href"));
urls.add(el.attr("href"));
}
if (relLinks.size() == 0){
Log.i(TAG, "0 image");
currentReportString.setImageBackground(Boolean.FALSE);
currentReportString.setImageBackground2(Boolean.FALSE);
currentReportString.setImageBackground3(Boolean.FALSE);
}
else if (relLinks.size() == 1) {
// Log.i(TAG, String.valueOf(urls));
currentReportString.setUrlImages(urls.get(0));
Log.i(TAG, "first URL : " + currentReportString.getUrlImages());
currentReportString.setImageBackground2(Boolean.FALSE);
currentReportString.setImageBackground3(Boolean.FALSE);
/* new DownloadImageTask(mImageReport)
.execute(mCurrentReportString.getUrlImages());*/
}
else if (relLinks.size() == 2){
currentReportString.setUrlImages(urls.get(0));
currentReportString.setUrlImages2(urls.get(1));
currentReportString.setImageBackground3(Boolean.FALSE);
Log.i(TAG, "first URL : "+currentReportString.getUrlImages());
Log.i(TAG, "second URL : "+currentReportString.getUrlImages2());
/* new DownloadImageTask(mImageReport)
.execute(mCurrentReportString.getUrlImages());
new DownloadImageTask(mImageReport2)
.execute(mCurrentReportString.getUrlImages2());*/
}
else if (relLinks.size() == 3){
currentReportString.setUrlImages(urls.get(0));
currentReportString.setUrlImages2(urls.get(1));
currentReportString.setUrlImages3(urls.get(2));
Log.i(TAG, "first URL : "+currentReportString.getUrlImages());
Log.i(TAG, "second URL : "+currentReportString.getUrlImages2());
Log.i(TAG, "third URL : "+currentReportString.getUrlImages3());
/* new DownloadImageTask(mImageReport2)
.execute(mCurrentReportString.getUrlImages2());
new DownloadImageTask(mImageReport3)
.execute(mCurrentReportString.getUrlImages3());*/
}
else if (relLinks.size() > 3){
currentReportString.setUrlImages(urls.get(0));
currentReportString.setUrlImages2(urls.get(1));
currentReportString.setUrlImages3(urls.get(2));
Log.i(TAG, "first URL : "+currentReportString.getUrlImages());
Log.i(TAG, "second URL : "+currentReportString.getUrlImages2());
Log.i(TAG, "third URL : "+currentReportString.getUrlImages3());
/* new DownloadImageTask(mImageReport)
.execute(mCurrentReportString.getUrlImages());
new DownloadImageTask(mImageReport2)
.execute(mCurrentReportString.getUrlImages2());
new DownloadImageTask(mImageReport3)
.execute(mCurrentReportString.getUrlImages3());*/
}
return currentReportString;
}
private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()) {
isAvailable = true;
}
return isAvailable;
}
//Delete HTML Markup
public String stripHtml(String html) {
return Html.fromHtml(html).toString();
}
private void alertUserAboutError() {
AlertDialogFragment dialog = new AlertDialogFragment();
dialog.show(getFragmentManager(), "error_dialog");
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
//Change Image from ImageView in Asynch with URL.
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
/* @Override
public void onTabSelected(android.support.v7.app.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(android.support.v7.app.ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(android.support.v7.app.ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}*/
private void initUI(){
final android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(android.support.v7.app.ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(this,
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener((android.support.v7.app.ActionBar.TabListener) this));
}
}
/* 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;
}*/
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jardelcompany.bundoransurfco" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" /> <!-- To retrieve the account name (email) as part of sign-in: -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> <!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:label="@string/title_activity_login"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize|stateVisible" >
</activity>
</application>
</manifest>
styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
SectionsPagerAdapter.java:
import java.util.Locale;
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;
public SectionsPagerAdapter(Context context, FragmentManager fm) {
super(fm);
mContext = context;
}
@Override
public Fragment getItem(int position) {
switch(position) {
case 0:
return new FirstFragment();
case 1:
return new LoginFragment();
}
return null;
}
@Override
public int getCount() {
return 2;
}
@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);
}
return null;
}
}
LoginFragment.java:
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class LoginFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_login,
container, false);
return rootView;
}
}
activity_login.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="#FFFFFF"
tools:context="jardelcompany.bundoransurfco.LoginFragment">
</RelativeLayout>
FirstFragment.java:
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main,
container, false);
return rootView;
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/mainLayout"
android:background="#FFFFFF"> />
<view
android:layout_width="match_parent"
android:layout_height="match_parent"
class="android.support.v4.view.ViewPager"
android:id="@+id/pager"/>
</RelativeLayout>
Logcat崩溃:
FATAL EXCEPTION: main
Process: jardelcompany.bundoransurfco, PID: 2250
java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
答案 0 :(得分:1)
由于错误的参数,您的MainActivity中的onResume(Bundle savedInstanceState)
方法永远不会被调用,而且您不会调用super.onResume()
。正确的方法是:
@Override
protected void onResume()
{
super.onResume();
initUI();
}
请参阅此处获取一些文档:http://developer.android.com/training/basics/activity-lifecycle/pausing.html#Resume
答案 1 :(得分:1)
按如下方式更新您的代码,
@Override
protected void onResume() {
super.onResume();
initUI();
}
答案 2 :(得分:1)
对于ActionBarActivity
,您应该使用getSupportFragmentManager()
和getSupportActionBar()