当我在slidemenu中启动片段但得到错误

时间:2016-02-18 11:24:11

标签: android

请帮助

Submitcv.java 这是导航slidemenu(submitcv)的片段,但是当我从mainactivity执行片段时我遇到了错误。但是当我使用这些片段时 没有任何代码,它被执行 和logcat没有任何显示任何错误

package com.example.app.slidemenu;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Locale;
    import com.example.app_placement.R;
    import android.app.DatePickerDialog;
    import android.app.DatePickerDialog.OnDateSetListener;
    import android.app.Fragment;
    import android.os.Bundle;
    import android.text.InputType;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.ArrayAdapter;
    import android.widget.DatePicker;
    import android.widget.EditText;
    import android.widget.Spinner;

    public class Submit_resume extends Fragment implements OnClickListener
    {


    //    EditText name,emailid,dateofbirth,location,currentsalary,mobilenumber;
    //    RadioGroup gendergroup;
    //    RadioButton male,female;
    //    Button submitcv;

        Spinner spinner;
        String string_array[];
        ArrayAdapter<String> arrayadapter;
        EditText dateofbirth;
        private DatePickerDialog fromDatePickerDialog;
        private SimpleDateFormat dateFormatter;
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState)
        {

          View rootView = inflater.inflate(R.layout.submit_resume, container, false);
    //      name=(EditText)container.findViewById(R.id.sname);
    //      emailid=(EditText)container.findViewById(R.id.sbtmail);
    //      dateofbirth=(EditText)container.findViewById(R.id.sdob);
    //      location=(EditText)container.findViewById(R.id.slocation);
    //      currentsalary=(EditText)container.findViewById(R.id.currentsalary);
    //      mobilenumber=(EditText)container.findViewById(R.id.smobile);
    //      gendergroup=(RadioGroup)container.findViewById(R.id.subgender);
    //      submitcv=(Button)container.findViewById(R.id.submitcv);
    //      Submit_cv();

          spinner=(Spinner)container.findViewById(R.id.spinner1);
            //dateofbirth=(EditText)findViewById(R.id.sdob);
            string_array=getResources().getStringArray(R.array.spinner_array);

            arrayadapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,
                  string_array);

             spinner.setAdapter(arrayadapter);
    //          Calendar c = Calendar.getInstance();
    //          SimpleDateFormat ss = new SimpleDateFormat("dd-MM-yyyy");
    //          Date date = new Date();
    //          String currentdate= ss.format(date);
               // dob=(EditText)findViewById(R.id.sdob);
                //dob.setText(currentdate);

             spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> arg0, View arg1,
                            int position, long id) { 
                        // TODO Auto-generated method stub
                       // spin_val = gender[position];//saving the value selected


                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub

                    }

                });



             dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);

             findViewsById();

                setDateTimeField();

          return rootView;

        }

        private void findViewsById()
        {
            dateofbirth = (EditText)getView().findViewById(R.id.sdob);  
            dateofbirth.setInputType(InputType.TYPE_NULL);
            dateofbirth.requestFocus();


        }

        private void setDateTimeField() {
            dateofbirth.setOnClickListener(this);


            Calendar newCalendar = Calendar.getInstance();
            fromDatePickerDialog = new DatePickerDialog(getActivity(), new OnDateSetListener() {

                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    Calendar newDate = Calendar.getInstance();
                    newDate.set(year, monthOfYear, dayOfMonth);
                    dateofbirth.setText(dateFormatter.format(newDate.getTime()));
                }

            },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));


        }



        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            if(v == dateofbirth) {
                fromDatePickerDialog.show();
            } 
        }

    //  public void Submit_cv()
    //    {
    //      
    //      System.out.println("value of usetname: "+name.getText().toString());
    //      
    //      final String gender = ((RadioButton)getView().findViewById(gendergroup.getCheckedRadioButtonId() )).getText().toString();
    //      System.out.println(gender);
    //    }


        }

mainactivity.java
这是主要活动,其中定义滑块代码并定义了滑块的所有片段。但是当使用的是除了初始片段膨胀方法之外的任何代码的片段。我在片段中使用任何东西     package com.example.app_placement;

import info.androidhive.slidingmenu.adapter.NavDrawerListAdapter;
import info.androidhive.slidingmenu.model.NavDrawerItem;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
import com.example.app.slidemenu.*;


@SuppressLint("NewApi")
public class MainActivity extends Activity {

    // Dialog choicedialog;


    ShareActionProvider mshareactioprovider;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    // nav drawer title
    private CharSequence mDrawerTitle;

    // used to store app title
    private CharSequence mTitle;

    // slide menu items
    private String[] navMenuTitles;
    private TypedArray navMenuIcons;

    private ArrayList<NavDrawerItem> navDrawerItems;
    private NavDrawerListAdapter adapter;
    private LinearLayout drawerll;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView userTextView = (TextView) findViewById(R.id.tvsideUserName);

        System.out.println("value of user from db:"+Constants.USERNAME);
        userTextView.setText(Constants.USERNAME);
        mTitle = mDrawerTitle = getTitle();

        // load slide menu items
        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

        // nav drawer icons from resources
        navMenuIcons = getResources()
                .obtainTypedArray(R.array.nav_drawer_icons);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
        drawerll = (LinearLayout) findViewById(R.id.drawerll);

        navDrawerItems = new ArrayList<NavDrawerItem>();

        // adding nav drawer items to array
        // Home
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons
                .getResourceId(0, -1)));
        // Find People
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons
                .getResourceId(1, -1)));
        // Photos
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons
                .getResourceId(2, -1)));
        // Communities, Will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons
                .getResourceId(3, -1), true, "22"));
        // Pages
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons
                .getResourceId(4, -1)));
        // What's hot, We will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons
                .getResourceId(5, -1), true, "50+"));

        // Recycle the typed array
        navMenuIcons.recycle();

        mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

        // setting the nav drawer list adapter
        adapter = new NavDrawerListAdapter(getApplicationContext(),
                navDrawerItems);
        mDrawerList.setAdapter(adapter);

        // enabling action bar app icon and behaving it as toggle button
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.menu1, // nav menu toggle icon
                R.string.app_name, // nav drawer open - description for
                                    // accessibility
                R.string.app_name // nav drawer close - description for
                                    // accessibility
        ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null)
        {
            // on first time display view for first nav item
            displayView(0);
        }
    }

    /**
     * Slide menu item click listener
     * */
    private class SlideMenuClickListener implements
            ListView.OnItemClickListener 
            {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // display view for selected nav drawer item
            displayView(position);
        }
    }

    public boolean onOptionsItemSelected(MenuItem item) 
    {
        // toggle nav drawer on selecting action bar app icon/title
        if (mDrawerToggle.onOptionsItemSelected(item)) 
        {
            return true;
        }
        // Handle action bar actions click
        switch (item.getItemId())
        {
        case R.id.action_settings:
            return true;
        case R.id.logout:

            startActivity(new Intent(this,Login_Activity.class));
            return true;
        case R.id.profile:
            return true;
        case R.id.abouts:
            //startActivity(new Intent(this,About_us.class));
            return true;
        case R.id.changepassword:
            return true;        
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        // Inflating the current activity's menu with
        getMenuInflater().inflate(R.menu.main, menu);

    //  getActionBar();
         ActionBar b=getActionBar();

         b.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.SteelBlue)));
         getActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
         //two more option to set bckcolor
         //bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
          // b.setBackgroundDrawable(new ColorDrawable(0x3366FF));


        // Getting the actionprovider associated with the menu item whose id is
        // share
        // mshareactioprovider=(ShareActionProvider)menu.findItem(R.id.newshare).getActionProvider();

        // Setting a share intent
        // mshareactioprovider.setShareIntent(getDefaultShareIntent());
        // Log.d("MainMenu", ",menu title0: " + menu.getItem(0).getTitle());
        // returns "New Form"
        // menu.addSubMenu(0, Menu.NONE, 1, "Form 1").setIcon(R.drawable.share
        // );
        // menu.addSubMenu(0, Menu.NONE, 2, "Form 2").setIcon(R.drawable.share);
        return true;
    }

        private Intent getDefaultShareIntent() {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT");
        intent.putExtra(Intent.EXTRA_TEXT, "Extra Text");
        return intent;

    }

    /* *
     * Called when invalidateOptionsMenu() is triggered
     */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // if nav drawer is opened, hide the action items
        final boolean drawerOpen = mDrawerLayout.isDrawerOpen(drawerll);
        // boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    /**
     * Diplaying fragment view for selected nav drawer list item
     * */
    private void displayView(int position) {
        // update the main content by replacing fragments
        Fragment fragment = null;
        switch (position) {
        case 0:
            fragment = new HomeFragment();
            break;
        case 1:
            fragment = new CurrentOpening();
            break;
        case 2:
             fragment = new  Client_Fragment();
            break;
        case 3:
            System.out.println("Submit fragment is called");
            fragment = new Submit_resume();
            break;
        case 4:
            fragment = new About_us();
            break;
        case 5:
            fragment = new Contact_us();
            break;

        default:
            break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container1, fragment).commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(navMenuTitles[position]);
            mDrawerLayout.closeDrawer(drawerll);
        } else {
            // error in creating fragment
            Log.e("MainActivity", "Error in creating fragment");
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }

    /**
     * When using the ActionBarDrawerToggle, you must call it during
     * onPostCreate() and onConfigurationChanged()...
     */

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    public void onBackPressed() {
        if (getFragmentManager().getBackStackEntryCount() > 0) {
            getFragmentManager().popBackStack();
        } else {
            super.onBackPressed();
        }
    }





}

1 个答案:

答案 0 :(得分:1)

您需要更改以下行:

发件人

 spinner=(Spinner)container.findViewById(R.id.spinner1);

spinner=(Spinner)rootView.findViewById(R.id.spinner1);

原因:您需要将您的UI小部件与片段视图绑定,而不是 Viewgroup

完成