Reputation: 51
I'm fairly new to Android programming and in general my programming skills are lacking. I was following a Head Start tutorial even though I know it had technically been removed to be updated or something. anyways I got up to a part where they just want you to copy and paste a code to get the NASA RSS feed and the android application to communicate with one another, but for some or other reason nothing displays. I just get a white screen and the logcat errors seem to not do much to others apps from what I have seen. the test data I used worked so I think the xml's are fine so maybe it is my Java. I cant seem to find anything though. Can anyone see what I am doing wrong. Been trying all weekend. It could be the copy pasted code, but I don't understand it.
package love.android.nasadailyimage;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.jar.Attributes;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class NasaDailyImage extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nasa_daily_image);
IotdHandler handler = new IotdHandler();
handler.processFeed();
resetDisplay(handler.getTitle(), handler.getDate(), handler.getImage(), handler.getDescription());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_nasa_daily_image, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class IotdHandler extends DefaultHandler {
private String url = "http://www.nasa.gov/rss/dyn/image_of_the_day.rss";
private boolean inUrl = false;
private boolean inTitle = false;
private boolean inDescription = false;
private boolean inItem = false;
private boolean inDate = false;
private Bitmap image = null;
private String title = null;
private StringBuffer description = null;
private String date = null;
public void processFeed() {
try {
SAXParserFactory factory =
SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.setContentHandler(this);
InputStream inputStream = new URL(url).openStream();
reader.parse(new InputSource(inputStream));
} catch (Exception e) {
}
}
private Bitmap getBitmap(String url) {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
input.close();
return bitmap;
} catch (IOException ioe) {
return null;
}
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equals("url")) {
inUrl = true;
} else {
inUrl = false;
}
if (localName.startsWith("item")) {
inItem = true;
} else if (inItem) {
if (localName.equals("title")) {
inTitle = true;
} else {
inTitle = false;
}
if (localName.equals("description")) {
inDescription = true;
} else {
inDescription = false;
}
if (localName.equals("pubDate")) {
inDate = true;
} else {
inDate = false;
}
}
}
public void characters(char ch[], int start, int length) {
String chars = new String(ch).substring(start, start + length);
if (inUrl && url == null) {
image = getBitmap(chars);
}
if (inTitle && title == null) {
title = chars;
}
if (inDescription) {
description.append(chars);
}
if (inDate && date == null) {
date = chars;
}
}
public Bitmap getImage() {
return image;
}
public String getTitle() {
return title;
}
public StringBuffer getDescription() {
return description;
}
public String getDate() {
return date;
}
}
private void resetDisplay(String title, String date, Bitmap image, StringBuffer description){
TextView titleView = (TextView) findViewById(R.id.imageTitle);
titleView.setText(title);
TextView dateView = (TextView) findViewById(R.id.imageDate);
dateView.setText(date);
ImageView imageView = (ImageView) findViewById(R.id.imageDisplay);
imageView.setImageBitmap(image);/////////////////////////////////////////
TextView descriptionView = (TextView) findViewById(R.id.imageDescription);
descriptionView.setText(description);
}
}
XML in case it helps
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".NasaDailyImage">
<TextView
android:id="@+id/imageTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/image_title" />
<TextView
android:id="@+id/imageDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/image_date" />
<ImageView
android:id="@+id/imageDisplay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/galaxy"
android:scaleType="fitXY"
android:adjustViewBounds="true"/>
<TextView
android:id="@+id/imageDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/image_description" />
</LinearLayout>
</ScrollView>
Any help would be really appreciated. I would ask someone here, but in my country 'Namibia' there are very few people who do android programming that I have access to.
my LogCat from my S4 device
04-20 11:07:17.204 25660-25660/love.android.nasadailyimage W/IInputConnectionWrapper﹕ getExtractedText on inactive InputConnection
04-20 11:07:17.204 25660-25660/love.android.nasadailyimage W/IInputConnectionWrapper﹕ getTextBeforeCursor on inactive InputConnection
04-20 11:07:17.209 25660-25660/love.android.nasadailyimage W/IInputConnectionWrapper﹕ getSelectedText on inactive InputConnection
04-20 11:07:17.209 25660-25660/love.android.nasadailyimage W/IInputConnectionWrapper﹕ getTextAfterCursor on inactive InputConnection
04-20 11:12:38.244 25660-25660/love.android.nasadailyimage W/IInputConnectionWrapper﹕ getExtractedText on inactive InputConnection
04-20 11:12:38.244 25660-25660/love.android.nasadailyimage W/IInputConnectionWrapper﹕ getTextBeforeCursor on inactive InputConnection
04-20 11:12:38.244 25660-25660/love.android.nasadailyimage W/IInputConnectionWrapper﹕ getSelectedText on inactive InputConnection
04-20 11:12:38.249 25660-25660/love.android.nasadailyimage W/IInputConnectionWrapper﹕ getTextAfterCursor on inactive InputConnection
04-20 11:16:06.154 30742-30742/love.android.nasadailyimage W/ApplicationPackageManager﹕ getCSCPackageItemText()
04-20 11:16:06.154 30742-30742/love.android.nasadailyimage I/PersonaManager﹕ getPersonaService() name persona_policy
04-20 11:16:06.229 30742-30742/love.android.nasadailyimage I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
04-20 11:16:06.229 30742-30742/love.android.nasadailyimage W/dalvikvm﹕ VFY: unable to resolve virtual method 12208: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
04-20 11:16:06.229 30742-30742/love.android.nasadailyimage D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
04-20 11:16:06.229 30742-30742/love.android.nasadailyimage I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
04-20 11:16:06.229 30742-30742/love.android.nasadailyimage W/dalvikvm﹕ VFY: unable to resolve virtual method 12214: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
04-20 11:16:06.229 30742-30742/love.android.nasadailyimage D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
04-20 11:16:06.234 30742-30742/love.android.nasadailyimage I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
04-20 11:16:06.234 30742-30742/love.android.nasadailyimage W/dalvikvm﹕ VFY: unable to resolve virtual method 9779: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
04-20 11:16:06.234 30742-30742/love.android.nasadailyimage D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
04-20 11:16:06.244 30742-30742/love.android.nasadailyimage I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
04-20 11:16:06.244 30742-30742/love.android.nasadailyimage W/dalvikvm﹕ VFY: unable to resolve virtual method 392: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
04-20 11:16:06.244 30742-30742/love.android.nasadailyimage D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
04-20 11:16:06.244 30742-30742/love.android.nasadailyimage I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
04-20 11:16:06.244 30742-30742/love.android.nasadailyimage W/dalvikvm﹕ VFY: unable to resolve virtual method 414: Landroid/content/res/TypedArray;.getType (I)I
04-20 11:16:06.244 30742-30742/love.android.nasadailyimage D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
04-20 11:16:06.244 30742-30742/love.android.nasadailyimage I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
04-20 11:16:06.249 30742-30742/love.android.nasadailyimage W/dalvikvm﹕ VFY: unable to resolve virtual method 355: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
04-20 11:16:06.249 30742-30742/love.android.nasadailyimage D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
04-20 11:16:06.249 30742-30742/love.android.nasadailyimage I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
04-20 11:16:06.249 30742-30742/love.android.nasadailyimage W/dalvikvm﹕ VFY: unable to resolve virtual method 357: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
04-20 11:16:06.249 30742-30742/love.android.nasadailyimage D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
04-20 11:16:06.369 30742-30742/love.android.nasadailyimage D/dalvikvm﹕ GC_FOR_ALLOC freed 130K, 6% free 16994K/18052K, paused 15ms, total 15ms
04-20 11:16:06.389 30742-30742/love.android.nasadailyimage I/dalvikvm-heap﹕ Grow heap (frag case) to 28.887MB for 11943952-byte allocation
04-20 11:16:06.404 30742-30751/love.android.nasadailyimage D/dalvikvm﹕ GC_FOR_ALLOC freed 4K, 4% free 28654K/29720K, paused 12ms, total 12ms
04-20 11:16:06.479 30742-30742/love.android.nasadailyimage I/System.out﹕ main(HTTPLog):SmartBonding Enabling is false, log to file is false, DBG is false
04-20 11:16:06.594 30742-30742/love.android.nasadailyimage D/OpenGLRenderer﹕ Enabling debug mode 0
AndroidManifest as requested
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="love.android.nasadailyimage" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".NasaDailyImage"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "love.android.nasadailyimage"
minSdkVersion 11
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
}
EDIT: added my LogCat and my AndroidManifest
Upvotes: 2
Views: 206
Reputation: 20513
Found a question on StackOverflow, that followed the same tutorial as you. Maybe this might help you
Android Head First "NASA daily image App"
As mentioned you should use AsyncTask, and process the request when it comes back (onPostExecute). The problem with your code is, that you send an request, and right after you call resetDisplay, and the response hasn't come back yet... that's why you see a blank screen.
Upvotes: 1
Reputation:
Your problem is probably in your build.gradle file, check the dependencies, you'll see something like this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
}
You can try copy pasting that over your dependencies.
Upvotes: 0