片段没有在Android中启动其他片段

时间:2014-01-09 05:09:37

标签: android android-fragments android-fragmentactivity

当用户第一次访问我的应用程序时,我有一个登录片段我曾经拿它的用户名和密码并将其保存到SharedPref。所以下次他不需要再次输入用户名和密码。

但是发生了什么事情并没有显示任何事情它只是显示一个空白活动,即使它没有显示的布局设计。

我的登录FragmentActivity代码

public class Login extends FragmentActivity {
    public static String url = "";
    public static ProgressDialog progressDialog;
    EditText username, password;
    TextView error;
    ImageView ok;
    static String AppResponse = null;
    ProgressDialog pd;
    private Fragment dashboardFragment;
    View rootView = null;

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

        SharedPreferences shared = getSharedPreferences("shared", MODE_PRIVATE);
        dashboardFragment = new DashBoardFragment();
        if (shared.contains("username") && shared.contains("password")) {

            startingActivity();
        } else {
            progressDialog = new ProgressDialog(this);
            progressDialog.setMessage("Please Wait Validating .!!!");
            progressDialog.setIndeterminate(false);
            progressDialog.setProgressStyle(ProgressDialog.THEME_HOLO_DARK);
            progressDialog.setCancelable(false);
            rootView = inflater.inflate(R.layout.activity_login, container,
                    false);
            username = (EditText) rootView.findViewById(R.id.textUserName);
            password = (EditText) rootView.findViewById(R.id.textPassword);
            ok = (ImageView) rootView.findViewById(R.id.btn_login);
            error = (TextView) rootView.findViewById(R.id.error_view);
            pd = new ProgressDialog(this);
            pd.setMessage("Logging In");
            ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String pass = password.getText().toString();
                    String userId = username.getText().toString();
                    boolean isConnected = isNetworkConnected(Login.this);
                    Log.d("Network Status", "Status is " + isConnected);
                    if (userId.equals("") || pass.equals("")) {
                        error.setText("Please provide UserName and Password...!!!!!");
                    } else {
                        if (isConnected) {
                            postHttpRequest(userId, pass, error);
                        } else {
                            Toast.makeText(getApplicationContext(),
                                    "No Network Connection", Toast.LENGTH_LONG)
                                    .show();
                        }
                    }
                }
            });
        }
        return rootView;
    }

StartActivity

public void startingActivity() {
        FragmentTransaction fragTran = getSupportFragmentManager()
                .beginTransaction();
        fragTran.replace(R.id.content_frame, dashboardFragment);
        fragTran.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        fragTran.addToBackStack(null);
        fragTran.commit();
    }

DashBoardFragment

public class DashBoardFragment extends Fragment {
    GridView gridView;
    static final String[] DASHBOARD_LINKS = new String[] { };
    private Fragment approvalStatusFragment;
    private Fragment pendingApprovalsFragment;
    private Fragment flghtBookingFragment;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_dash_board,
                container, false);
        gridView = (GridView) rootView.findViewById(R.id.my_grid_view);
        gridView.setAdapter(new ImageAdapter(getActivity(), DASHBOARD_LINKS));

        approvalStatusFragment = new ApprovalStatus();
        pendingApprovalsFragment = new PendingApprovals();
        flghtBookingFragment = new FlightBooking();

        final FragmentTransaction fragTran = getFragmentManager()
                .beginTransaction();

        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int arg2,
                    long arg3) {
                if (DASHBOARD_LINKS[arg2].equals("Approvals")) {
                    fragTran.replace(R.id.content_frame, pendingApprovalsFragment);
                    fragTran.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    fragTran.addToBackStack(null);
                    fragTran.commit();
                }
                if (DASHBOARD_LINKS[arg2].equals("Flights")) {
                    fragTran.replace(R.id.content_frame, flghtBookingFragment);
                    fragTran.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    fragTran.addToBackStack(null);
                    fragTran.commit();
                }
                if (DASHBOARD_LINKS[arg2].equals("Trips")) {
                    fragTran.replace(R.id.content_frame, approvalStatusFragment);
                    fragTran.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    fragTran.addToBackStack(null);
                    fragTran.commit();
                }
            }
        });
        return rootView;
    }
}

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.iween.newinfiniteapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

   <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.Login"
            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>

我收到此异常

01-09 11:26:01.162: E/AndroidRuntime(4164): FATAL EXCEPTION: main
01-09 11:26:01.162: E/AndroidRuntime(4164): java.lang.IllegalArgumentException: No view found for id 0x7f09001e (com.iween.newinfiniteapp:id/content_frame) for fragment DashBoardFragment{412cfcf8 #0 id=0x7f09001e}
01-09 11:26:01.162: E/AndroidRuntime(4164):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
01-09 11:26:01.162: E/AndroidRuntime(4164):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
01-09 11:26:01.162: E/AndroidRuntime(4164):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-09 11:26:01.162: E/AndroidRuntime(4164):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
01-09 11:26:01.162: E/AndroidRuntime(4164):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
01-09 11:26:01.162: E/AndroidRuntime(4164):     at android.os.Handler.handleCallback(Handler.java:800)
01-09 11:26:01.162: E/AndroidRuntime(4164):     at android.os.Handler.dispatchMessage(Handler.java:100)
01-09 11:26:01.162: E/AndroidRuntime(4164):     at android.os.Looper.loop(Looper.java:194)
01-09 11:26:01.162: E/AndroidRuntime(4164):     at android.app.ActivityThread.main(ActivityThread.java:5371)
01-09 11:26:01.162: E/AndroidRuntime(4164):     at java.lang.reflect.Method.invokeNative(Native Method)
01-09 11:26:01.162: E/AndroidRuntime(4164):     at java.lang.reflect.Method.invoke(Method.java:525)
01-09 11:26:01.162: E/AndroidRuntime(4164):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
01-09 11:26:01.162: E/AndroidRuntime(4164):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-09 11:26:01.162: E/AndroidRuntime(4164):     at dalvik.system.NativeStart.main(Native Method)

* MainFragment *

public class MainFragment extends FragmentActivity {
    // Within which the entire activity is enclosed
    private DrawerLayout mDrawerLayout;

    // ListView represents Navigation Drawer
    private ListView mDrawerList;
    private Fragment dashboardFragment;

    // ActionBarDrawerToggle indicates the presence of Navigation Drawer in the
    // action bar
    private ActionBarDrawerToggle mDrawerToggle;

    // Title of the action bar
    private String mTitle = "";

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_fragment);


        dashboardFragment = new DashBoardFragment();


        mTitle = "Infinite Travels";
        getActionBar().setTitle(mTitle);
        getActionBar().setIcon(android.R.color.transparent);

        // Getting reference to the DrawerLayout
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.drawer_list);

        // Getting reference to the ActionBarDrawerToggle
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.white_bar, R.string.drawer_open,
                R.string.drawer_close) {

            /** Called when drawer is closed */
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu();
            }

            /** Called when a drawer is opened */
            public void onDrawerOpened(View drawerView) {
                // getActionBar().setTitle("");
                invalidateOptionsMenu();
            }
        };

        // Setting DrawerToggle on DrawerLayout
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        if (savedInstanceState == null) {
            getUrl(0);
        }
        // Creating an ArrayAdapter to add items to the listview mDrawerList
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                getBaseContext(), R.layout.drawer_list_item, getResources()
                        .getStringArray(R.array.menus));

        // Setting the adapter on mDrawerList
        mDrawerList.setAdapter(adapter);

        // Enabling Home button
        getActionBar().setHomeButtonEnabled(true);

        // Enabling Up navigation
        getActionBar().setDisplayHomeAsUpEnabled(true);

        // Setting item click listener for the listview mDrawerList
        mDrawerList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // Getting an array of rivers
                String[] menuItems = getResources().getStringArray(
                        R.array.menus);

                // Currently selected river
                mTitle = menuItems[position];
                getUrl(position);

                // Closing the drawer
                mDrawerLayout.closeDrawer(mDrawerList);
            }
        });
    }

    @SuppressLint("Recycle")
    protected void getUrl(int position) {
        FragmentTransaction fragTran = getSupportFragmentManager()
                .beginTransaction();
        switch (position) {
        case 0:
            fragTran.replace(R.id.content_frame, dashboardFragment);
            fragTran.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            fragTran.addToBackStack(null);
            fragTran.commit();
            break;
        case 1:

        case 2:

        case 3:
            // return "";
        case 4:

        case 5:

        case 6:
            // return "";
        default:
            // return "";
        }
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * Called whenever we call invalidateOptionsMenu()
     * */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);

        menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

问题是它显示空白页面

2 个答案:

答案 0 :(得分:0)

你的类应该是这样的(FragmentActivity没有OnCreateView方法,它有OnCreate方法)所以只需将方法OnCrateView更改为OnCreate。

public class Login extends FragmentActivity {
        public static String url = "";
        public static ProgressDialog progressDialog;
        EditText username, password;
        TextView error;
        ImageView ok;
        static String AppResponse = null;
        ProgressDialog pd;
        private Fragment dashboardFragment;
        View rootView = null;
        @Override
        protected Void onCreate(Bundle savedInstanceState) {

            SharedPreferences shared = getSharedPreferences("shared", MODE_PRIVATE);
            dashboardFragment = new DashBoardFragment();
            if (shared.contains("username") && shared.contains("password")) {

                startingActivity();
            } else {
                progressDialog = new ProgressDialog(this);
                progressDialog.setMessage("Please Wait Validating .!!!");
                progressDialog.setIndeterminate(false);
                progressDialog.setProgressStyle(ProgressDialog.THEME_HOLO_DARK);
                progressDialog.setCancelable(false);
                setContentView(R.layout.activity_login);

                username = (EditText) findViewById(R.id.textUserName);
                password = (EditText) findViewById(R.id.textPassword);
                ok = (ImageView) findViewById(R.id.btn_login);
                error = (TextView) findViewById(R.id.error_view);
                pd = new ProgressDialog(this);
                pd.setMessage("Logging In");
                ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String pass = password.getText().toString();
                        String userId = username.getText().toString();
                        boolean isConnected = isNetworkConnected(Login.this);
                        Log.d("Network Status", "Status is " + isConnected);
                        if (userId.equals("") || pass.equals("")) {
                            error.setText("Please provide UserName and Password...!!!!!");
                        } else {
                            if (isConnected) {
                                postHttpRequest(userId, pass, error);
                            } else {
                                Toast.makeText(getApplicationContext(),
                                        "No Network Connection", Toast.LENGTH_LONG)
                                        .show();
                            }
                        }
                    }
                });
            }

        }

答案 1 :(得分:0)

由于您的static final String[] DASHBOARD_LINKS = new String[] { };数组不包含任何值,因此这不是将任何数据绑定到GridView的原因。

将一些数据添加到String数组中,然后将其绑定到GridView适配器。