0

我制作了一个可以从网上下载图像的android应用程序,但是当我运行它时它会停止。这里没有错误是我的主类 MainActivity.class `package com.downloadfiles;

public class MainActivity extends Activity {

    Button btnShowProgress;
    private ProgressDialog pDialog;
    ImageView my_image;


    EditText edt = (EditText) findViewById(R.id.editText1);


    public static final int progress_bar_type = 0; 


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

        String i = edt.getText().toString();
        final String file_url  = i ;

        btnShowProgress = (Button) findViewById(R.id.btnProgressBar);

        my_image = (ImageView) findViewById(R.id.my_image);

        btnShowProgress.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                new DownloadFileFromURL().execute(file_url);
            }
        });
    }


    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case progress_bar_type:
            pDialog = new ProgressDialog(this);
            pDialog.setMessage("Downloading file Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setMax(100);
            pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            pDialog.setCancelable(true);
            pDialog.show();
            return pDialog;
        default:
            return null;
        }
    }


    class DownloadFileFromURL extends AsyncTask<String, String, String> {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
    showDialog(progress_bar_type);
        }


        @Override
        protected String doInBackground(String... f_url) {
            int count;
            try {
                URL url = new URL(f_url[0]);
                URLConnection conection = url.openConnection();
                conection.connect();

                int lenghtOfFile = conection.getContentLength();

                String mak;
                InputStream input = new BufferedInputStream(url.openStream(), 8192);
                String foe; 
                EditText form = (EditText) findViewById(R.id.formet);
                mak = form.getText().toString();
                foe = "Download file.";
                  OutputStream output = new FileOutputStream("/sdcard/download/" +foe +mak);


                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;

                    publishProgress(""+(int)((total*100)/lenghtOfFile));
                   output.write(data, 0, count);

                }

                output.flush();
                output.close();

                input.close();

            } catch (Exception e) {
                Log.e("Error: ", e.getMessage());
            }

            return null;
        }


        protected void onProgressUpdate(String... progress) {
            // setting progress percentage
            pDialog.setProgress(Integer.parseInt(progress[0]));
       }


        @Override
        protected void onPostExecute(String file_url) {

            dismissDialog(progress_bar_type);
             String mak;
             EditText form = (EditText) findViewById(R.id.formet);
              mak = form.getText().toString();

            if (mak =="jpg")
              {
                String imagePath = Environment.getExternalStorageDirectory().toString() + "/download/Download file." +"jpg";
                my_image.setImageDrawable(Drawable.createFromPath(imagePath));
              }
            else if (mak == "png")
              {
                String imagePath = Environment.getExternalStorageDirectory().toString() + "/download/Download file." +"png";
                my_image.setImageDrawable(Drawable.createFromPath(imagePath));
              }
            else if (mak == "gif"){
                String imagePath = Environment.getExternalStorageDirectory().toString() + "/download/Download file."+"gif";
                my_image.setImageDrawable(Drawable.createFromPath(imagePath));
            }
            else
              {Intent toNextPage = new Intent(MainActivity.this,
                        backgo.class); 
                startActivity(toNextPage);
    }
            }}}`    

这是我的另一堂课 backgo.class

           `public class backgo extends Activity{
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sorryerror);
    Button btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
    public void onClick(View v) {
            Intent toNextPage = new Intent(backgo.this,
                    MainActivity.class); 
            startActivity(toNextPage);

        }
    });}}`

这是我的布局 activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"

    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="13dp"
        android:text="Enter the formet in which you want to download the file i.e mp3,jpg or mp4:"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/formet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPostalAddress" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Enter the url from where you want to download file:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:text = "http://images6.fanpop.com/image/photos/33400000/Naruto-evoliution-naruto-33413374-1600-1152.png"
        android:inputType="textPostalAddress" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btnProgressBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:text="Download File " />

    <ImageView android:id="@+id/my_image"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>



</LinearLayout>

这是我的另一个布局sorryerror.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sorry the formet of your image you requested is not supported by the application we are very sorry..."
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30dp"
        android:textStyle="bold"
        android:typeface="serif" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="233dp"
        android:layout_weight="0.37"
        android:src="@drawable/crying" />

    <Button
        android:id="@+id/button1"
        android:layout_width="166dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.01"
        android:text="Click here to go back" />

</LinearLayout>

请帮助提前谢谢。

4

4 回答 4

1

首先,修改类中的EditTextMainActivity如下:

private EditText edt;

然后将此行放入您的onCreate方法中:

edt = (EditText) findViewById(R.id.editText1);

现在它应该可以工作了。

于 2013-05-30T07:34:29.310 回答
0
EditText edt = (EditText) findViewById(R.id.editText1); //<<<

您正在尝试在调用setContentViewActivity 之前初始化 EditText。所以在为 Activty 设置布局后,在 onCreate 中初始化 EditText。

EditText edt; //<< Declare here
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  edt = (EditText) findViewById(R.id.editText1); //<< initilze here
  String i = edt.getText().toString();
  //...

编辑:-String i = edt.getText().toString();如果您想获取用户在 EditText 中输入的文本,请在按钮的 onClick 中进行更多操作:

my_image = (ImageView) findViewById(R.id.my_image);
btnShowProgress.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String file_url = edt.getText().toString();

        new DownloadFileFromURL().execute(file_url);
    }
});
于 2013-05-30T07:24:45.277 回答
0

edt之后 初始化setContentView_onCreate()

   public class MainActivity extends Activity {

        Button btnShowProgress;
        private ProgressDialog pDialog;
        ImageView my_image;


        EditText edt ;


        public static final int progress_bar_type = 0; 


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

            //Initialize editText Here        
            edt= (EditText) findViewById(R.id.editText1);

            String i = edt.getText().toString();
.......
于 2013-05-30T07:25:06.983 回答
0
String i = edt.getText().toString();

这是你的问题。您从未初始化edt.

你错过了edt = (EditText) findViewById(R.id.editText1)

如果 (mak =="jpg")

java中String的内容需要equalsequalsIgnoreCase为了与另一个String进行比较。使用 ==将比较字符串的地址。

改进去

if (mak.equals("jpg"))
于 2013-05-30T07:23:42.597 回答