2013-05-30 22 views
0

我做了一個Android應用程序,可以從網上下載圖像,但是當我運行它,它stops.It沒有錯誤,這裏是我的主類MainActivity.class`包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> 

請幫助謝謝提前。

回答

0
String i = edt.getText().toString(); 

這是你的問題。您從未初始化edt

你錯過了edt = (EditText) findViewById(R.id.editText1)

如果(MAK == 「JPG」)

String的Java中的內容需要以equalsequalsIgnoreCase與另一個字符串進行比較。使用==將比較字符串的地址。

改變它在

if (mak.equals("jpg")) 
+0

感謝,但同時在圖像視圖中顯示圖像,我有地方,如果其他條件 – Blacklotis

+0

你可以點線我的申請仍然停止? – Blackbelt

+0

是backgo.class活動?你有沒有在AndroidManifest.xml文件中聲明它? – Blackbelt

0
EditText edt = (EditText) findViewById(R.id.editText1); //<<< 

你試圖呼喚setContentView的活動之前初始化的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(); 
    //... 

編輯: -在巴頓的onClick也更String i = edt.getText().toString();如果你想獲得通過用戶的EditText作爲文本輸入:edtsetContentView

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); 
    } 
}); 
+0

謝謝,但我的應用程序仍然停止,而在圖像視圖顯示圖像,我已經放置瞭如果其他條件 – Blacklotis

+0

@ user2435339:好吧然後顯示你的logcat結果當應用程序崩潰 –

+0

感謝它現在的作品 – Blacklotis

0

初始化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(); 
....... 
1

首先,修改喲EditText線烏爾MainActivity類,如下所示:

private EditText edt; 

然後把這個線你onCreate方法:

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

現在,它應該工作。

+0

謝謝它現在的作品,但我的應用程序仍然停止,而在圖像視圖顯示圖像,我已經放置瞭如果其他條件,你可以請幫我 – Blacklotis

+0

我認爲你解決了你的問題。 – muratgurbuzdal

相關問題