无法在片段

时间:2016-06-18 15:50:29

标签: java android android-fragments

我创建了一个名为 HomeFragment 的片段,它实现了swipeRefreshLayout。
我正在尝试做的是,我从服务器获取一个json数组,我从json中提取信息,并使用 HomeFragment 中的自定义适配器扩展数组适配器来扩充布局,我也有一个 MenuActivity.java ,在线性布局中有5个按钮,当点击按钮时,片段会发生变化 现在,当我运行应用程序时,按钮不会在点击时更改片段,也不会使布局膨胀。

logcat的

06-18 11:16:02.803 994-994/com.example.udeys.theindianroute E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.udeys.theindianroute.MenuActivity.access$super
06-18 11:16:02.813 994-994/com.example.udeys.theindianroute E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.udeys.theindianroute.MenuActivity.access$super
06-18 11:16:02.813 994-994/com.example.udeys.theindianroute E/dalvikvm: Could not find class 'android.media.session.MediaController', referenced from method com.example.udeys.theindianroute.MenuActivity.access$super
06-18 11:16:02.813 994-994/com.example.udeys.theindianroute E/dalvikvm: Could not find class 'android.widget.Toolbar', referenced from method com.example.udeys.theindianroute.MenuActivity.access$super
06-18 11:16:02.813 994-994/com.example.udeys.theindianroute E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method com.example.udeys.theindianroute.MenuActivity.access$super
06-18 11:16:02.823 994-994/com.example.udeys.theindianroute E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.udeys.theindianroute.MenuActivity.access$super
06-18 11:16:02.823 994-994/com.example.udeys.theindianroute E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.udeys.theindianroute.MenuActivity.access$super
06-18 11:16:02.823 994-994/com.example.udeys.theindianroute E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.udeys.theindianroute.MenuActivity.access$super
06-18 11:16:02.833 994-994/com.example.udeys.theindianroute E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method com.example.udeys.theindianroute.MenuActivity.access$super
06-18 11:16:02.833 994-994/com.example.udeys.theindianroute E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method com.example.udeys.theindianroute.MenuActivity.access$super
06-18 11:16:02.833 994-994/com.example.udeys.theindianroute E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.example.udeys.theindianroute.MenuActivity.access$super
06-18 11:16:02.973 994-994/com.example.udeys.theindianroute E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.udeys.theindianroute.EmptyFragment.access$super
06-18 11:16:02.973 994-994/com.example.udeys.theindianroute E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.udeys.theindianroute.EmptyFragment.access$super
06-18 11:16:02.983 994-994/com.example.udeys.theindianroute E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.udeys.theindianroute.HomeFragment.access$super
06-18 11:16:02.983 994-994/com.example.udeys.theindianroute E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.example.udeys.theindianroute.HomeFragment.access$super
06-18 11:16:03.033 994-994/com.example.udeys.theindianroute E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.udeys.theindianroute, PID: 994
                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.udeys.theindianroute/com.example.udeys.theindianroute.MenuActivity}: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:136)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                                                  at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                  at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                                                  at dalvik.system.NativeStart.main(Native Method)
                                                                               Caused by: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
                                                                                  at android.app.ListFragment.ensureList(ListFragment.java:402)
                                                                                  at android.app.ListFragment.onViewCreated(ListFragment.java:203)
                                                                                  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
                                                                                  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
                                                                                  at android.app.BackStackRecord.run(BackStackRecord.java:684)
                                                                                  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
                                                                                  at android.app.Activity.performStart(Activity.java:5240)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                  at android.os.Looper.loop(Looper.java:136) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                                                  at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                  at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                                                  at dalvik.system.NativeStart.main(Native Method) 


MenuActivity.java

package com.example.udeys.theindianroute;

import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;

public class MenuActivity extends AppCompatActivity implements View.OnClickListener {


    ImageButton hm, srch, pst, notif, prfl;
    FragmentTransaction ft;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);
        initFragments();

        hm = (ImageButton) findViewById(R.id.home);
        srch = (ImageButton) findViewById(R.id.trip);
        pst = (ImageButton) findViewById(R.id.post);
        notif = (ImageButton) findViewById(R.id.notification);
        prfl = (ImageButton) findViewById(R.id.profile);


        hm.setOnClickListener(this);
        srch.setOnClickListener(this);
        pst.setOnClickListener(this);
        notif.setOnClickListener(this);
        prfl.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        int id = v.getId();

        switch (id) {
            case R.id.home:
                ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.fragment_1, new HomeFragment());
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
                break;
            case R.id.trip:
                ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.fragment_1, new TripFragment());
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
                break;
            case R.id.post:
                ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.fragment_1, new PostFragment());
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
                break;
            case R.id.notification:
                ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.fragment_1, new NotificationFragment());
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
                break;
            case R.id.profile:
                ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.fragment_1, new ProfileFragment());
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
                break;
        }

    }

    private void initFragments() {
        ft = getFragmentManager().beginTransaction();
        ft.add(R.id.fragment_1, new HomeFragment());
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        //ft.hide(mGalleryFragment);
        ft.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_activity, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:
                // User chose the "Settings" item, show the app settings UI...
                return true;


            default:
                // If we got here, the user's action was not recognized.
                // Invoke the superclass to handle it.
                return super.onOptionsItemSelected(item);

        }
    }
}


HomeFragment.java

package com.example.udeys.theindianroute;

/**
 * Created by udeys on 6/17/2016.
 */

import android.app.Fragment;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.udeys.theindianroute.utils.PostAdapter;
import com.example.udeys.theindianroute.utils.posts;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;

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

import cz.msebera.android.httpclient.Header;

public class HomeFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
    PostAdapter PostAdapter;
    private SwipeRefreshLayout swipeRefreshLayout;
    ListView datalist;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.homefragment, container, false);
        datalist = (ListView) view.findViewById(R.id.users_posts);
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout);
        return view;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        datalist.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (datalist.getChildAt(0) != null) {
                    swipeRefreshLayout.setEnabled(datalist.getFirstVisiblePosition() == 0 && datalist.getChildAt(0).getTop() == 0);
                }
            }
        });
        Typeface cFont = Typeface.createFromAsset(getActivity().getAssets(), "SAMARN__.TTF");
        Typeface fontAwesome = Typeface.createFromAsset(getActivity().getAssets(), "fontawesome-webfont.ttf");
        PostAdapter = new PostAdapter(getActivity(), R.layout.timelinerowlayout, cFont, fontAwesome);
        swipeRefreshLayout.setOnRefreshListener(this);
        /**
         * Showing Swipe Refresh animation on activity create
         * As animation won't start on onCreate, post runnable is used
         */
        swipeRefreshLayout.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        swipeRefreshLayout.setRefreshing(true);
                                        bindData();
                                    }
                                }
        );

    }

    /**
     * This method is called when swipe refresh is pulled down
     */
    @Override
    public void onRefresh() {
        bindData();
    }


    public void bindData() {


        try {
            AsyncHttpClient client = new AsyncHttpClient(true, 80, 443);
            client.get("http://indianroute.roms4all.com/fetch_post.php", new TextHttpResponseHandler() {
                        @Override
                        public void onSuccess(int statusCode, Header[] headers, String res) {
                            decodeJson(res);

                        }

                        @Override
                        public void onFailure(int statusCode, Header[] headers, String res, Throwable t) {
                            // called when response HTTP status is "4XX" (eg. 401, 403, 404)
                            Toast.makeText(getActivity(), "" + res, Toast.LENGTH_SHORT).show();
                        }
                    }
            );
        } catch (Exception e) {
            Toast.makeText(getActivity(), "" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }


    }

    private void decodeJson(String result) {
        try {
            swipeRefreshLayout.setRefreshing(true);
            JSONArray jArr = new JSONArray(result);
            String username, story, picture, check_in, userprofilePicture;
            for (int count = 0; count < jArr.length(); count++) {
                JSONObject obj = jArr.getJSONObject(count);
                username = obj.getString("username");
                story = obj.getString("story");
                picture = obj.getString("picture");
                check_in = obj.getString("check_in");
                userprofilePicture = obj.getString("userprofilePicture");
                posts posts = new posts(username, story, picture, check_in, userprofilePicture);
                swipeRefreshLayout.setRefreshing(false);
                PostAdapter.add(posts);
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}


homefragment.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:context="com.example.gitesh.timeline.MainActivity">

        <ListView
            android:id="@+id/users_posts"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

</android.support.v4.widget.SwipeRefreshLayout>

0 个答案:

没有答案