2013-07-02 6 views
7

मैं नक्शे apiv2 से निपट रहा हूं। और डायलॉग फ्रैगमेंट क्लास के लिए कोडिंग करते समय मुझे निम्न त्रुटि मिल रही है।खंडों में गैर-डिफ़ॉल्ट कन्स्ट्रक्टर त्रुटि

public PlaceDialogFragment(){ 
    super(); 
} 

public PlaceDialogFragment(Place place, DisplayMetrics dm){ 
    super(); 
    this.mPlace = place; 
    this.mMetrics = dm; 
} 

किसी भी मदद की सराहना की wud ..

संशोधित:

Error :Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead:

इस कोड है इस पूरे कोड है ... मैं एक नौसिखिया की तरह हूँ और कभी नहीं टुकड़ों से निपटने से पहले..मैं एक नक्शा टट के माध्यम से जा रहा था और पाया ... क्या आप अब विस्तार से बता सकते हैं कि आप समझाने की कोशिश कर रहे थे ..

package com.example.travelplanner; 

import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.URL; 

import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v4.app.DialogFragment; 
import android.util.DisplayMetrics; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.ViewFlipper; 

// Defining DialogFragment class to show the place details with photo 
public class PlaceDialogFragment extends DialogFragment{ 

    TextView mTVPhotosCount = null; 
    TextView mTVVicinity = null; 
    ViewFlipper mFlipper = null; 
    Place mPlace = null; 
    DisplayMetrics mMetrics = null; 

    public PlaceDialogFragment(){ 
     super(); 
    } 

    public PlaceDialogFragment(Place place, DisplayMetrics dm){ 
     super(); 
     this.mPlace = place; 
     this.mMetrics = dm; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     // For retaining the fragment on screen rotation 
     setRetainInstance(true); 
     super.onCreate(savedInstanceState); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.dialog_layout, null); 

     // Getting reference to ViewFlipper 
     mFlipper = (ViewFlipper) v.findViewById(R.id.flipper); 

     // Getting reference to TextView to display photo count 
     mTVPhotosCount = (TextView) v.findViewById(R.id.tv_photos_count); 

     // Getting reference to TextView to display place vicinity 
     mTVVicinity = (TextView) v.findViewById(R.id.tv_vicinity); 

     if(mPlace!=null){ 

      // Setting the title for the Dialog Fragment 
      getDialog().setTitle(mPlace.mPlaceName); 

      // Array of references of the photos 
      Photo[] photos = mPlace.mPhotos; 

      // Setting Photos count 
      mTVPhotosCount.setText("Photos available : " + photos.length); 

      // Setting the vicinity of the place 
      mTVVicinity.setText(mPlace.mVicinity); 

      // Creating an array of ImageDownloadTask to download photos 
      ImageDownloadTask[] imageDownloadTask = new ImageDownloadTask[photos.length]; 

      int width = (int)(mMetrics.widthPixels*3)/4; 
      int height = (int)(mMetrics.heightPixels*1)/2; 

      String url = "https://maps.googleapis.com/maps/api/place/photo?"; 
      String key = "key=AIzaSyBSo1xlML-tOFTCdJb6qIbVnF9e7y5nj0o"; 
      String sensor = "sensor=true"; 
      String maxWidth="maxwidth=" + width; 
      String maxHeight = "maxheight=" + height; 
      url = url + "&" + key + "&" + sensor + "&" + maxWidth + "&" + maxHeight; 

      // Traversing through all the photoreferences 
      for(int i=0;i<photos.length;i++){ 
       // Creating a task to download i-th photo 
       imageDownloadTask[i] = new ImageDownloadTask(); 

       String photoReference = "photoreference="+photos[i].mPhotoReference; 

       // URL for downloading the photo from Google Services 
       url = url + "&" + photoReference; 

       // Downloading i-th photo from the above url 
       imageDownloadTask[i].execute(url); 
      } 
     } 
     return v; 
    } 

    @Override 
    public void onDestroyView() { 
     if (getDialog() != null && getRetainInstance()) 
      getDialog().setDismissMessage(null); 
      super.onDestroyView(); 
    } 

    private Bitmap downloadImage(String strUrl) throws IOException{ 
     Bitmap bitmap=null; 
     InputStream iStream = null; 
     try{ 
      URL url = new URL(strUrl); 

      /** Creating an http connection to communcate with url */ 
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

      /** Connecting to url */ 
      urlConnection.connect(); 

      /** Reading data from url */ 
      iStream = urlConnection.getInputStream(); 

      /** Creating a bitmap from the stream returned from the url */ 
      bitmap = BitmapFactory.decodeStream(iStream); 

     }catch(Exception e){ 
      Log.d("Exception while downloading url", e.toString()); 
     }finally{ 
      iStream.close(); 
     } 
     return bitmap; 
    } 

    private class ImageDownloadTask extends AsyncTask<String, Integer, Bitmap>{ 
     Bitmap bitmap = null; 
     @Override 
     protected Bitmap doInBackground(String... url) { 
      try{ 
       // Starting image download 
       bitmap = downloadImage(url[0]); 
      }catch(Exception e){ 
       Log.d("Background Task",e.toString()); 
      } 
      return bitmap; 
     } 

     @Override 
     protected void onPostExecute(Bitmap result) { 
      // Creating an instance of ImageView to display the downloaded image 
      ImageView iView = new ImageView(getActivity().getBaseContext()); 

      // Setting the downloaded image in ImageView 
      iView.setImageBitmap(result); 

      // Adding the ImageView to ViewFlipper 
      mFlipper.addView(iView); 

      // Showing download completion message 
      Toast.makeText(getActivity().getBaseContext(), "Image downloaded successfully", Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 
+0

त्रुटि पंक्ति सुपर पर PlaceDialogFragment की पैरामिट्रीकृत निर्माता में लागू किया जाता है के लिए काम किया(); – Divyang

+0

मैंने कुछ उत्तरों की जांच की और पाया कि यदि आप कन्स्ट्रक्टर से सुपर() को हटाते हैं तो इसे ठीक काम करना चाहिए ... – Vickyexpert

उत्तर

7

खंड की प्रकृति और आपकी स्क्रीन पर कैसे प्रबंधित और दिखाया गया है, इसकी अनुशंसा की जाती है कि गैर-डिफ़ॉल्ट कन्स्ट्रक्टर न बनाएं और कई मामलों में यह रन टाइम में समस्याएं पैदा करेगा। मामले में आप कुछ इस तरह करना चाहिए:

public static final GridFragment newInstance(String tabId) 
{ 
    GridFragment f = new GridFragment(); 
    Bundle bdl = new Bundle(2); 
    bdl.putString(TAB_ID, tabId); 
    f.setArguments(bdl); 
    return f; 
} 

और onCreate में बंडल से आवश्यक डेटा निकालें:

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    String tabId = getArguments().getString(TAB_ID); 
    } 

और साथ ही इस सवाल पर एक नज़र डालें:

Do fragments really need an empty constructor?

23

यदि आप नियमों से बाहर होना चाहते हैं तो बस अगले

@SuppressLint("ValidFragment") 
public PlaceDialogFragment(Place place, DisplayMetrics dm){ 
     super(); 
     this.mPlace = place; 
     this.mMetrics = dm; 
    } 
+0

त्रुटि: प्रतीक वर्ग नहीं मिल सकता है SuppressLint –

3
@SuppressLint({"NewApi", "ValidFragment"}) 
public PlaceDialogFragment(Place place, DisplayMetrics dm){ 
     super(); 
     } 
+0

भविष्य में, अपने उत्तर के साथ स्पष्टीकरण प्रदान करना सुनिश्चित करें। – EternalHour

2

आसान तरीका Gradle में इन्हें जोड़ने के लिए है:

android { 
     lintOptions { 
      checkReleaseBuilds false 
     } 
    } 

या आप प्रत्येक खंड

@SuppressLint("ValidFragment") 
public PlaceDialogFragment(Place place, DisplayMetrics dm){ 
    super(); 
    this.mPlace = place; 
    this.mMetrics = dm; 
} 

बेस्ट समाधान बस Gradle में लाइनों को जोड़ने है में इस लाइन जोड़ सकते हैं।

0

समाधान मुझे

android { 
      lintOptions { 
       checkReleaseBuilds false 
      } 
     } 
संबंधित मुद्दे