错误的片段非默认的构造函数 [英] Error non-default constructors in fragments

查看:127
本文介绍了错误的片段非默认的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处理与地图apiv2。而我收到以下错误而编码对话片段类。

错误:避免非默认的构造函数的片段:使用默认的构造函数加上片段#setArguments(捆绑)代替:

这是在code:

 公共PlaceDialogFragment(){
    超();
}

公共PlaceDialogFragment(广场的地方,DisplayMetrics DM){
    超();
    this.mPlace =地方;
    this.mMetrics = DM;
}
 

任何帮助WUD是AP preciated ..

修订:这是整个code ......我米样的新手和从未有过处理fragments..I正在经历一个地图啧啧,发现this..can你现在细说笏ü试图解释..

 包com.example.travelplanner;

进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.net.HttpURLConnection中;
进口的java.net.URL;

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

//定义DialogFragment类来说明这个地方的细节与照片
公共类PlaceDialogFragment扩展DialogFragment {

    TextView的mTVPhotosCount = NULL;
    TextView的mTVVicinity = NULL;
    ViewFlipper mFlipper = NULL;
    将mPlace = NULL;
    DisplayMetrics mMetrics = NULL;

    公共PlaceDialogFragment(){
        超();
    }

    公共PlaceDialogFragment(广场的地方,DisplayMetrics DM){
        超();
        this.mPlace =地方;
        this.mMetrics = DM;
    }

    @覆盖
    公共无效的onCreate(包savedInstanceState){

        //对于保留在屏幕上旋转的片段
        setRetainInstance(真正的);
        super.onCreate(savedInstanceState);
    }

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                            捆绑savedInstanceState){
        视图V = inflater.inflate(R.layout.dialog_layout,NULL);

        //获取参考ViewFlipper
        mFlipper =(ViewFlipper)v.findViewById(R.id.flipper);

        //获取参考TextView中显示照片计数
        mTVPhotosCount =(TextView中)v.findViewById(R.id.tv_photos_count);

        //获取参考TextView的显示位置附近
        mTVVicinity =(TextView中)v.findViewById(R.id.tv_vicinity);

        如果(mPlace!= NULL){

            //设置的对话片段标题
            。getDialog()的setTitle(mPlace.mPlaceName);

            //照片的引用数组
            照片[]照片= mPlace.mPhotos;

            //设置照片计数
            mTVPhotosCount.setText(可用的照片:+ photos.length);

            //设置场所的附近
            mTVVicinity.setText(mPlace.mVicinity);

            //创建ImageDownloadTask阵列下载照片
            ImageDownloadTask [] imageDownloadTask =新ImageDownloadTask [photos.length]

            INT宽度=(int)的(mMetrics.widthPixels * 3)/ 4;
            INT高度=(int)的(mMetrics.heightPixels * 1)/ 2;

            字符串URL =htt​​ps://maps.googleapis.com/maps/api/place/photo?;
            字符串键=键= AIzaSyBSo1xlML-tOFTCdJb6qIbVnF9e7y5nj0o;
            弦传感器=传感器=真;
            字符串=了maxWidth=了maxWidth+宽;
            字符串=了maxHeight=了maxHeight+高度;
            URL =网址+&放大器; +按键+&放大器; +传感器+&放大器; + +了maxWidth与& +了maxHeight;

            //通过遍历所有的photoreferences
            的for(int i = 0; I< photos.length;我++){
                //创建任务,下载第i个照片
                imageDownloadTask [我] =新ImageDownloadTask();

                字符串photoReference =photoreference =+照片[I] .mPhotoReference;

                // URL下载从谷歌服务的照片
                URL =网址+&放大器; + photoReference;

                //下载第i个照片从上面的网址
                imageDownloadTask [I] .execute(URL);
            }
        }
        返回伏;
    }

    @覆盖
    公共无效onDestroyView(){
        如果(getDialog()= NULL和放大器;!&安培; getRetainInstance())
            getDialog()setDismissMessage(空)。
            super.onDestroyView();
    }

    私人位图downloadImage(字符串strUrl)抛出IOException异常{
        点阵位图= NULL;
        的InputStream的IStream = NULL;
        尝试{
            网址URL =新的URL(strUrl);

            / **创建HTTP连接communcate与URL * /
            HttpURLConnection的的URLConnection =(HttpURLConnection类)url.openConnection();

            / **连接到URL * /
            urlConnection.connect();

            从URL / **读取数据* /
            的IStream = urlConnection.getInputStream();

            / **创建位图从流从URL *返回/
            位= BitmapFactory.de codeStream(istream的);

        }赶上(例外五){
            Log.d(异常边下载网址,e.toString());
        }最后{
            iStream.close();
        }
        返回的位图;
    }

    私有类ImageDownloadTask扩展的AsyncTask<字符串,整数,位图> {
        点阵位图= NULL;
        @覆盖
        受保护的位图doInBackground(字符串... URL){
            尝试{
                //启动映像下载
                位图= downloadImage(网址[0]);
            }赶上(例外五){
                Log.d(后台任务,e.toString());
            }
            返回的位图;
        }

        @覆盖
        保护无效onPostExecute(位图的结果){
            //创建ImageView的实例显示下载的图像
            ImageView的iView中=新ImageView的(getActivity()getBaseContext());

            //设置在ImageView的下载图像
            iView.setImageBitmap(结果);

            //添加ImageView的到ViewFlipper
            mFlipper.addView(iView中);

            //显示下载完成消息
            Toast.makeText(getActivity()getBaseContext(),图像成功下载,Toast.LENGTH_SHORT).show();
        }
    }
}
 

解决方案

如果你想出来的规则,只是做下一个

  @燮pressLint(ValidFragment)
公共PlaceDialogFragment(广场的地方,DisplayMetrics DM){
        超();
        this.mPlace =地方;
        this.mMetrics = DM;
    }
 

I am dealing with maps apiv2. And I am getting the following error while coding for Dialog Fragment class.

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

This is the code :

public PlaceDialogFragment(){
    super();
}

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

Any help wud be appreciated..

Revised: This is the whole code...I m kind of a newbie and never before dealt with fragments..I was going through a maps tut and found this..can you elaborate now wat u were trying to explain..

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();
        }
    }
}

解决方案

if you like to be out of rules just do next

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

这篇关于错误的片段非默认的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆