2016-05-25 11 views
0

Ich weiß nicht, warum Variable myInt an einigen Stellen in meinem Code null ist.Passing-Parameter funktionieren nicht

In diesen Linien ist es in Ordnung, so variabel ist nicht null:

myInt = bundle2.getString("scelta2", null); 
titolo3.setText(myInt); 

Wenn ich Variable myInt in Paragraph p1=new Paragraph(myInt);, es ist null.

Was kann das Problem sein?

public class TwoFragment extends Fragment{ 

       private View v; 
       Intent chooser=null; 
       String myInt=""; 

       public TwoFragment() { 
        // Required empty public constructor 
       } 

       @Override 
       public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
       } 

       @Override 
       public View onCreateView(LayoutInflater inflater, ViewGroup container, 
             Bundle savedInstanceState) { 
        // Inflate the layout for this fragment 
        View rootView = inflater.inflate(R.layout.fragment_two, container, false); 
        Button mButton = (Button) rootView.findViewById(R.id.newbutton); 
        mButton.setOnClickListener(new View.OnClickListener() { 
         public void onClick(View v) { 

          //sendemail(); 
          createPDF(); 
          viewPDF(); 



         } 
        }); 
        TextView titolo3 = (TextView)rootView.findViewById(R.id.result); 
        TextView titolo2 = (TextView)rootView.findViewById(R.id.result2); 
        TextView titolo4 = (TextView)rootView.findViewById(R.id.resultpizze); 

        //TextView titolo = (TextView)rootView.findViewById(R.id.quantità3); 

        /* Bundle bundle2=getArguments(); 
        if(bundle2 != null){ 
         String string = bundle2.getString("scelta2", null); 
         titolo3.setText(string); 
        }*/ 

        Bundle bundle2=getArguments(); 
        if(bundle2 != null){ 
         myInt = bundle2.getString("scelta2", null); 
         titolo3.setText(myInt); 
        } 

        Bundle bundle3=getArguments(); 
        if(bundle3 != null){ 
         // String myInt3 = bundle3.getString("totalebirre", null); 
         // cazzo2=Integer.parseInt(myInt3); 
         int cazzo2=bundle3.getInt("totalebirre"); 
         titolo2.setText(String.valueOf(cazzo2)); 



        } 
        Bundle bundle=getArguments(); 
        if(bundle != null){ 
         // String myInt2 = bundle2.getString("totalepizze", null); 
         // cazzo=Integer.parseInt(myInt2); 
         //titolo2.setText(myInt2); 
         String string=bundle.getString("scelta3", null); 
         titolo4.setText(string); 

        } 



        return rootView; 
       } 
       /* public void sendemail(){ 

        Intent intent = new Intent(Intent.ACTION_SEND); 
        intent.setData(Uri.parse("mailto:")); 
        String[] to={"[email protected]"}; 
        intent.putExtra(Intent.EXTRA_EMAIL,to); 
        intent.putExtra(Intent.EXTRA_SUBJECT, "ciao"); 
        intent.putExtra(Intent.EXTRA_TEXT, "zao"); 
        intent.setType("message/rfc822"); 
        chooser=intent.createChooser(intent,"manda email"); 
        startActivity(chooser); 
       }*/ 
       public void createPDF() { 

        Document doc = new Document(); 

        try { 
         String path = Environment.getExternalStorageDirectory() 
           .getAbsolutePath() + "/droidText"; 

         File dir = new File(path); 
         if (!dir.exists()) 
          dir.mkdirs(); 

         Log.d("PDFCreator", "PDF Path: " + path); 

         File file = new File(dir, "sample.pdf"); 
         FileOutputStream fOut = new FileOutputStream(file); 

         PdfWriter.getInstance(doc, fOut); 

         // open the document 
         doc.open(); 
         ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
         Bitmap bitmap = BitmapFactory.decodeResource(getContext() 
           .getResources(), R.drawable.androtuto); 
         bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
         Image myImg = Image.getInstance(stream.toByteArray()); 
         myImg.setAlignment(Image.MIDDLE); 

         // add image to document 
         doc.add(myImg); 

         Paragraph p1 = new Paragraph(myInt); 
        Log.d("ciao",myInt); 

         Font paraFont = new Font(Font.COURIER); 
         p1.setAlignment(Paragraph.ALIGN_CENTER); 
         p1.setFont(paraFont); 

         // add paragraph to document 
         doc.add(p1); 

         Paragraph p2 = new Paragraph("Ciao"); 
         Font paraFont2 = new Font(Font.COURIER, 14.0f, Color.GREEN); 
         p2.setAlignment(Paragraph.ALIGN_CENTER); 
         p2.setFont(paraFont2); 

         doc.add(p2); 

         stream = new ByteArrayOutputStream(); 
         bitmap = BitmapFactory.decodeResource(getContext() 
           .getResources(), R.drawable.android); 
         bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
         myImg = Image.getInstance(stream.toByteArray()); 
         myImg.setAlignment(Image.MIDDLE); 

         // add image to document 
         doc.add(myImg); 

         // set footer 
         Phrase footerText = new Phrase("Pied de page "); 
         HeaderFooter pdfFooter = new HeaderFooter(footerText, false); 
         doc.setFooter(pdfFooter); 

        } catch (DocumentException de) { 
         Log.e("PDFCreator", "DocumentException:" + de); 
        } catch (IOException e) { 
         Log.e("PDFCreator", "ioException:" + e); 
        } finally { 
         doc.close(); 
        } 

       } 
       public void viewPDF(){ 
        String path = "/sdcard/droidText/sample.pdf"; 
        File targetFile = new File(path); 
        Uri targetUri = Uri.fromFile(targetFile); 

        Intent intent; 
        intent = new Intent(Intent.ACTION_VIEW); 
        intent.setDataAndType(targetUri, "application/pdf"); 

        startActivity(intent); 
       } 

      } 

FRAGMENT von wo ich VARIABLES PASS:

public class ThreeFragment extends Fragment implements 
     android.widget.CompoundButton.OnCheckedChangeListener { 

    ListView lv2; 
    ArrayList<Planet> planetList; 
    ListView lv; 

    ArrayList<Birra> birraList; 
    BirraAdapter biAdapter; 
    PlanetAdapter plAdapter; 
    Planet p; 
    String myInt=""; 



    PlanetAdapter.PlanetHolder holder; 


    public ThreeFragment() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 

     final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_three, container, false); 

     Button mButton = (Button) rootView.findViewById(R.id.button2); 
     mButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       MyListFragment mlf=new MyListFragment(); 

       mlf.showResult(v); 
       // MyListFragment.showResult(v); 
       showResult2(v); 


      } 
     }); 
     //return inflater.inflate(R.layout.fragment_list2, container, false); 
     return rootView; 
    } 


    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 

     lv2 = (ListView) getView().findViewById(R.id.listview2); 
     displayBirraList(); 
    } 


    private void displayBirraList() { 

     birraList = new ArrayList<Birra>(); 
     birraList.add(new Birra("Paulaner", 6, "€")); 
     birraList.add(new Birra("Forst", 7, "€")); 
     birraList.add(new Birra("Peroni", 5, "€")); 
     birraList.add(new Birra("Corona", 5, "€")); 
     birraList.add(new Birra("Nastro Azzurro", 4, "€")); 
     biAdapter = new BirraAdapter(birraList, getContext()) { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       int pos = lv2.getPositionForView(buttonView); 
       if (pos != ListView.INVALID_POSITION) { 
        Birra b = birraList.get(pos); 
        b.setSelected(isChecked); 


      /*Toast.makeText(
       getActivity(), 
       "Clicked on Pizza: " + p.getName() + ". State: is " 
         + isChecked, Toast.LENGTH_SHORT).show();*/ 
       } 


      } 


     }; 


     lv2.setAdapter(biAdapter); 
    } 


    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 


     /*int pos = lv.getPositionForView(buttonView); 
     if (pos != ListView.INVALID_POSITION) { 
      Planet p = planetList.get(pos); 
      p.setSelected(isChecked); 


      *//**//**//**//*Toast.makeText(
       getActivity(), 
       "Clicked on Planet: " + p.getName() + ". State: is " 
         + isChecked, Toast.LENGTH_SHORT).show();*//**//**//**//* 
     } 
*/ 
    } 


    public void showResult2(View v) { 
     String result = "Selected Product are :"; 
     int totalAmount = 0; 
     // String a = ""; 


     for (Birra b : biAdapter.getBox()) { 

      if (b.selected) { 

       result += "\n" + b.name + " " + b.distance + "€" + "q.tà :" + b.getQuantità(); 
       int quantitaInt = Integer.parseInt(b.getQuantità()); 
       totalAmount += b.distance * quantitaInt; 
       // a=String.valueOf(totalAmount); 


      } 
     } 
     /* for (Planet p : plAdapter.getBox()) { 
      if (p.isSelected()) { 


       result += "\n" + p.getName() + " " + p.getDistance() + "€" + "q.tà :" + p.getQuantità(); 
       int quantitaInt = Integer.parseInt(p.getQuantità()); 
       //totalAmount2+=p.distance * quantitaInt; 
       //z=String.valueOf(totalAmount2); 


      } 
     }*/ 



     Toast.makeText(getActivity(), result + "\n" +myInt + "\n" + "Total Amount:=" + totalAmount + "€", Toast.LENGTH_LONG).show(); 
      Bundle bun2 = new Bundle(); 
      bun2.putString("scelta2", result); 
      TwoFragment fgsearch2 = new TwoFragment(); 
      fgsearch2.setArguments(bun2); 
      android.support.v4.app.FragmentTransaction transaction2 = getActivity().getSupportFragmentManager().beginTransaction(); 
      transaction2.replace(R.id.content_main, fgsearch2); 
      transaction2.commit(); 

      Bundle bun = new Bundle(); 
      // bun.putString("totalebirre", a); 
     bun.putInt("totalebirre", totalAmount); 
      TwoFragment fgsearch = new TwoFragment(); 
      fgsearch.setArguments(bun); 
      android.support.v4.app.FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction(); 
      transaction.replace(R.id.content_main2, fgsearch); 
      transaction.commit(); 




    } 
} 
+0

Teilen Sie den Code der Klasse von wo Sie es weitergeben –

+0

Was sollte in Ihrem 'Titolo3'' TextView' angezeigt werden? –

+0

Nur eine Randnotiz: Sie müssen nicht drei Instanzen des Bundles mit getArguments() erhalten. Benutze einfach das gleiche Bündel. –

Antwort

0

Ok endlich richtig Sie keine Argumente übergeben:

public static TwoFragment newInstance(int yourInt) { 
    TwoFragment f = new TwoFragment(); 
    // Supply index input as an argument. 
    Bundle args = new Bundle(); 
    args.putInt("myInt", yourInt); 
    f.setArguments(args); 
    return f; 
} 

dann in onCreateView bekommen int mit

Bundle args = getArguments(); 
if(args != null) 
    myInt = args.getInt("myInt", 0); 

und machen su re Sie mit Fragment-Instanz erstellen:

TwoFragment yourfragment = TwoFragment.newInstance(123); //here pass your int 

Sie haben:

TwoFragment fgsearch2 = TwoFragment.newInstance(result); 

Schließlich ersetzen:

Bundle bun2 = new Bundle(); 
      bun2.putString("scelta2", result); 
      TwoFragment fgsearch2 = new TwoFragment(); 
      fgsearch2.setArguments(bun2); 
      android.support.v4.app.FragmentTransaction transaction2 = getActivity().getSupportFragmentManager().beginTransaction(); 
      transaction2.replace(R.id.content_main, fgsearch2); 
      transaction2.commit(); 

mit:

Bundle bun2 = new Bundle(); 
bun2.putString("scelta2", result); 
TwoFragment fgsearch2 = new TwoFragment(); 
fgsearch2.setArguments(bun2); 

Sie so etwas wie ändern muss

+0

muss ich den Parameter "myInt" endgültig machen? – Angus

+0

wenn ich letzten String schreibe myInt = ""; Ich habe ein Problem mit dieser Zeile: myInt = bundle2.getString ("scelta2", null); .... myInt ist in rot von IDE unterstrichen und es sagt mir: kann der Variablen – Angus

+0

public void createPDF (final int yourIntHere) {... –