尝试在两个活动之间实施滑动,导致应用程序强制关闭

时间:2014-07-22 14:14:16

标签: android view swipe

我目前正在尝试在两个活动之间实施滑动手势。我有按钮操作的代码等。

我使用滑动代码创建了另一个项目,然后我的想法是加入两个项目(复制粘贴代码)。

我可以成功加入代码,但是当我运行应用程序时,我强制关闭,无法打开应用程序。

我已经搜索了几天而且找不到解决方案,如果有人可以帮助我会很棒。我将保留以下代码:

main_activity.java

package com.android.btree;



    public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = { "Registo", "Final"};
    String validacao;
    String TAG;
   int pAno;
   int pMes;
   int pDia;
   int pHora;
   int pMinuto;
   int pHoraF;
   int pMinutoF;
   static String verifdata = "Data";
   static String verifhorai = "Hora I.";
   static String verifhoraf = "Hora F.";      


   static final int DATE_DIALOG_ID = 999;
   static final int TIMEI_DIALOG_ID = 991;
   static final int TIMEF_DIALOG_ID = 992;

   DatePicker DataReg;
   TimePicker HorasReg;


   EditText ficha,recurso,ocorrencia,def_tipo,def_qtd,qtd_aprov,qtd_rej,qtd_prod;
   Button btVerificar;
   Button btData;
   Button btHoraI;
   Button btHoraF;
   Button btAddRec;
   Button btAddOcor;
   Button btAddDef;

   List<recurso> listrec = new ArrayList <recurso>();
   List<ocorrencia> listocor = new ArrayList <ocorrencia>();
   List<defeito> listdef = new ArrayList <defeito>();


   @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

 // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

    /**
     * on swiping the viewpager make respective tab selected
     * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });



     btVerificar = (Button) findViewById(R.id.buttonverif);
     btData =(Button) findViewById(R.id.buttondata);
     btHoraI = (Button) findViewById(R.id.buttonhorai);
     btHoraF =(Button) findViewById(R.id.buttonhoraf);
     btAddRec = (Button) findViewById(R.id.buttonadd1);
     btAddOcor = (Button) findViewById(R.id.buttonadd2);
     btAddDef = (Button) findViewById(R.id.buttonadd3);


     ficha = (EditText) findViewById(R.id.editTextficha);
     recurso = (EditText) findViewById(R.id.editTextrecurso);
     ocorrencia = (EditText) findViewById(R.id.editTextocorrencia);
     def_tipo = (EditText) findViewById(R.id.editTextdeftipo);
     def_qtd = (EditText) findViewById(R.id.editTextdefqtd);
     qtd_aprov = (EditText) findViewById(R.id.editTextqtda);
     qtd_rej = (EditText) findViewById(R.id.editTextqtdr);
     qtd_prod = (EditText) findViewById(R.id.editTextqtdp);


     btData.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                       showDialog(DATE_DIALOG_ID);

                }

         });

      btHoraI.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                       showDialog(TIMEI_DIALOG_ID);

                }

         });

      btHoraF.setOnClickListener(new OnClickListener(){
          @Override
          public void onClick(View v) {
              showDialog(TIMEF_DIALOG_ID);
          }
      });

      btAddRec.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String cod = recurso.getText().toString();
              recurso rec= new recurso(cod);
              listrec.add(rec);
              recurso.setText("");
        }
      });

      btAddOcor.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String cod = ocorrencia.getText().toString();
              ocorrencia ocor= new ocorrencia(cod);
              listocor.add(ocor);
              ocorrencia.setText("");
        }
        });

      btAddDef.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String tipo = def_tipo.getText().toString();
            String qtd = def_qtd.getText().toString();
            defeito def = new defeito(tipo,qtd);
            listdef.add(def);
            def_tipo.setText("");
            def_qtd.setText("");
        }
        });

      /** Get the current date */
      final Calendar cal = Calendar.getInstance();
      pAno = cal.get(Calendar.YEAR);
      pMes = cal.get(Calendar.MONTH);
      pDia = cal.get(Calendar.DAY_OF_MONTH);
      updateDisplay();

}


        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {

        }




       private void updateDisplay() {
      // TODO Auto-generated method stub
     btData.setText(new StringBuilder().append(pDia)
        .append("-").append(pMes+1).append("-").append(pAno)
        .append(" ")); 
        }




       public void verif_click(View v) {

     // TODO Auto-generated method stub
        ficha.setError(null);
        btData.setError(null);
        btHoraI.setError(null);
        btHoraF.setError(null);
        recurso.setError(null);
        ocorrencia.setError(null);
        def_tipo.setError(null);
        def_qtd.setError(null);
        qtd_aprov.setError(null);
        qtd_prod.setError(null);

        int flag=0;
        SimpleDateFormat  format = new SimpleDateFormat("dd-MM-yyyy");
        SimpleDateFormat formath = new SimpleDateFormat ("HH:mm");
        String fc = "";
        java.util.Date dt = null;
        java.util.Date hi = null;
        java.util.Date hf = null;
        String valorqa = qtd_aprov.getText().toString();
        String valorqp = qtd_prod.getText().toString();
        String valorfc = ficha.getText().toString();
  String valorqr = qtd_rej.getText().toString();
  String valordt = btData.getText().toString();
  String valorhi = btHoraI.getText().toString();
  String valorhf = btHoraF.getText().toString();
  Double qa = (double) 0.0;
  Double qp = (double)0.0;
  Double qr = (double) 0.0;
  Log.v(TAG, "valorqa: "+valorqa);
  try {
      Log.v(TAG, "valorqa: "+valorqa);
      qa=Double.valueOf(valorqa);
  }// try
  catch (NumberFormatException e) {
      Log.v(TAG, "qa: "+e);
      qtd_aprov.setError("Indique a quantidade aprovada");
      flag++;
  }
  try {
      Log.v(TAG, "valorqp: "+valorqp);
      qp=Double.valueOf(valorqp);
  }// try
  catch (NumberFormatException e) {
      Log.v(TAG, "qp: "+e);
      qtd_prod.setError("Indique a quantidade produzida");
      flag++;
  }
  try {
      Log.v(TAG, "valorqr: "+valorqr);
      qr=Double.valueOf(valorqr);
  }// try
  catch (NumberFormatException e) {
      Log.v(TAG, "qr: "+e);
      qr=qp-qa;
  }


  if ((valorfc == null) || (valorfc.length()==0)){
      ficha.setError("Indique a ficha");
      flag++;
  }
  else {
      fc=valorfc;
  }

      try {
        dt=format.parse(valordt);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
  if (valorhi.equals(verifhorai)){
      btHoraI.setError("Indique a hora inicial");
      flag++;
  }
  else {
      try {
          Log.v(TAG, "valor hora i: "+valorhi);
        hi=formath.parse(valorhi);
        Log.v(TAG, "hora i: "+hi);
  } catch (Exception e) {
        // TODO Auto-generated catch block
        Log.e(TAG, "erro: " +e);
    }
  }
  if (valorhf.equals(verifhoraf)){
      btHoraF.setError("Indique a hora final");
      flag++;
  }
  else {
      try {
        hf=formath.parse(valorhf);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
  }
  if(listrec.isEmpty()) {
      recurso.setError("Adicione o(s) recurso(s)");
      flag++;
  }


  if (flag==0){  
      final registo reg = new registo(fc, dt, hi, hf, qa,qr, qp, listrec, listocor, listdef);
      Log.v("Main","registo: "+reg.toString());
      Toast.makeText(this, "Ficha registada com sucesso!", Toast.LENGTH_SHORT).show();
      verifhorai = "Hora I.";
      verifhoraf = "Hora F.";

      DatabaseAdapter<?> bd = new DatabaseAdapter(this);
      bd.open();
      long nreg = bd.createFicha(reg);
      bd.close();
  }
  else {
      Toast.makeText(this, "Ficha não foi registada!", Toast.LENGTH_SHORT).show();
      verifhorai = "Hora I.";
      verifhoraf = "Hora F.";}

    ficha.getEditableText().clear();
    qtd_aprov.getEditableText().clear();
    qtd_rej.getEditableText().clear();
    qtd_prod.getEditableText().clear();
    btHoraI.setText("Hora I.");
    btHoraF.setText("Hora F.");
    listrec.clear(); 
    listocor.clear();
    listdef.clear();

};


@Override
protected Dialog onCreateDialog(int id) {
         switch (id) {
                case DATE_DIALOG_ID:
                       // set date picker as current date
                       final Calendar c = Calendar.getInstance();
                       pAno = c.get(Calendar.YEAR);
                       pMes = c.get(Calendar.MONTH);
                       pDia = c.get(Calendar.DAY_OF_MONTH);

                       return new DatePickerDialog(this, datePickerListener, pAno,   pMes,
                                    pDia);

                case TIMEI_DIALOG_ID:
                              // set date picker as current date
                       final Calendar c1 = Calendar.getInstance();
                       pHora = c1.get(Calendar.HOUR);
                       pMinuto = c1.get(Calendar.MINUTE);


                       return new TimePickerDialog(this, timePickerListener, pHora, pMinuto, true);


                case TIMEF_DIALOG_ID:
                              // set date picker as current date
                    final Calendar c2 = Calendar.getInstance();
                    pHoraF = c2.get(Calendar.HOUR);
                    pMinutoF = c2.get(Calendar.MINUTE);


                    return new TimePickerDialog(this, timePickerListener1, pHoraF, pMinutoF, true);
                }
         return null;
   }

   private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

         // when dialog box is closed, below method will be called.
         public void onDateSet(DatePicker view, int selectedYear,
                       int selectedMonth, int selectedDay) {
                pAno = selectedYear;
                pMes = selectedMonth;
                pDia = selectedDay;

                // set selected date into textview
                btData.setText(new StringBuilder().append(pDia)
                              .append("-").append(pMes+1).append("-").append(pAno)
                              .append(" "));            
         }
   };

   private OnTimeSetListener timePickerListener = new OnTimeSetListener() {

         // when dialog box is closed, below method will be called.
         public void onTimeSet(TimePicker view, int selectedHour,
                       int selectedMinute) {
                pHora = selectedHour;
                pMinuto = selectedMinute;

                // set selected date into textview
                btHoraI.setText(new StringBuilder().append(pHora)
                              .append(":").append(pMinuto));                   
         }
   };

   private OnTimeSetListener timePickerListener1 = new OnTimeSetListener() {

       // when dialog box is closed, below method will be called.
       public void onTimeSet(TimePicker view, int selectedHour,
                     int selectedMinute) {
              pHoraF = selectedHour;
              pMinutoF = selectedMinute;

              // set selected date into textview
              btHoraF.setText(new StringBuilder().append(pHoraF)
                            .append(":").append(pMinutoF));
       }
 };

TabsPagerApdater.java

package com.btreeapp.apdaters;


public class TabsPagerAdapter extends FragmentPagerAdapter {

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 0:
        // Top Rated fragment activity
        return new fragment_1();
    case 1:
        // Games fragment activity
        return new fragment_2();

    }

    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 2;
 }

}

Fragment_1.xml

package com.btreeapp.apdaters;


public class fragment_1 extends Fragment {

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

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

        return rootView;
    }
}

Fragment_2.xml

package com.btreeapp.apdaters;

public class fragment_2 extends Fragment {

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

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

        return rootView;
    }
}

0 个答案:

没有答案