我是Android编程的新手。我正在尝试开发一个应用来查看月刊。
我尝试仅加载第一页。但是,当我单击“提交”按钮时,该应用程序将关闭。如果我删除图像加载部分,则该应用程序可以正常运行。
主要活动和显示活动的代码如下:
主要活动代码
package com.example.mariyanaadam;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button);
// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.spinner);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
// Spinner click listener
//spinner.setOnItemSelectedListener(this);
// Spinner Drop down elements
String [] months ={"January","February","March","April", "May","Jue","July","August","September","October","November","December"};
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,months);
spinner.setAdapter(adapter);
String [] years ={"2019"};
ArrayAdapter<String>adapter1=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,years);
spinner2.setAdapter(adapter1);
}
/** When the user click Submint button */
public void Display_actvity(View v)
{
Spinner spinner = (Spinner) findViewById(R.id.spinner);
int Month = spinner.getSelectedItemPosition() +1;
String mth = String.valueOf(Month);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
int pos = spinner2.getSelectedItemPosition(); // get selected uten oisutuib
String yr = (String) spinner2.getItemAtPosition(pos); //get intem name athe posiion
Intent intent = new Intent(getApplicationContext(), DisplayPage.class);
intent.putExtra("yr", yr);
intent.putExtra("mth", mth);
startActivity(intent);
/* Toast toast = Toast.makeText(getApplicationContext(),mth,Toast.LENGTH_LONG);
toast.show();*/
}
}
DISPLAY PAGE ACITIVITY CODE
package com.example.mariyanaadam;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.AsyncTask;
import android.widget.ImageView;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.*;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.TextView;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class DisplayPage extends AppCompatActivity {
public static final String URL = "http://www.geocities.ws/mariyanaatham/2019/may/1.png";
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_page);
Intent intent = getIntent();
String mth = intent.getStringExtra("mth");
Integer j = Integer.valueOf(mth);
String yr = intent.getStringExtra("yr");
String Month ="";
if((j<5) && (Integer.valueOf(yr)==2019))
{
Toast toast = Toast.makeText(getApplicationContext(),"Megazine Not Available",Toast.LENGTH_LONG);
toast.show();
}
switch(j){
case 1:
{
Month ="jan";
break;
}
case 2:
{
Month ="feb";
break;
}
case 3:
{
Month ="mar";
break;
}
case 4:
{
Month ="apr";
break;
}
case 5:
{
Month ="may";
break;
}
case 6:
{
Month ="jun";
break;
}
}
//Image loading section
imageView = (ImageView) findViewById(R.id.imageView);
// Create an object for subclass of AsyncTask
GetXMLTask task = new GetXMLTask();
// Execute the task
task.execute(new String[] { URL });
}
private class GetXMLTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... urls) {
Bitmap map = null;
for (String url : urls) {
map = downloadImage(url);
}
return map;
}
// Sets the Bitmap returned by doInBackground
@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
// Creates Bitmap from InputStream and returns it
private Bitmap downloadImage(String url) {
Bitmap bitmap = null;
InputStream stream = null;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
try {
stream = getHttpConnection(url);
bitmap = BitmapFactory.
decodeStream(stream, null, bmOptions);
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}
// Makes HttpURLConnection and returns InputStream
private InputStream getHttpConnection(String urlString)
throws IOException {
InputStream stream = null;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
try {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
stream = httpConnection.getInputStream();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return stream;
}
}
}
杂志的页面以png文件的形式存储在Web服务器中。当用户选择月份和年份然后单击提交按钮时,页面将以不同的图像视图加载到水平滚动条中。