我做了一個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>
請幫助謝謝提前。
感謝,但同時在圖像視圖中顯示圖像,我有地方,如果其他條件 – Blacklotis
你可以點線我的申請仍然停止? – Blackbelt
是backgo.class活動?你有沒有在AndroidManifest.xml文件中聲明它? – Blackbelt