Java 无法让Android教程工作,初学者程序员
我对Android编程相当陌生,一般来说,我缺乏编程技能。尽管我知道它在技术上已经被删除以进行更新,但我还是遵循了一个入门教程。无论如何,我得到了一个部分,他们只是想让你复制和粘贴一个代码,让NASA的RSS提要和android应用程序相互通信,但出于某种原因,什么也没有显示。我只是得到一个白色的屏幕和logcat错误似乎没有做太多的其他应用程序从我所看到的。我使用的测试数据有效,所以我认为xml很好,所以可能是我的Java。但我似乎什么也找不到。谁能看出我做错了什么。我整个周末都在努力。可能是复制粘贴的代码,但我不明白Java 无法让Android教程工作,初学者程序员,java,android,xml,Java,Android,Xml,我对Android编程相当陌生,一般来说,我缺乏编程技能。尽管我知道它在技术上已经被删除以进行更新,但我还是遵循了一个入门教程。无论如何,我得到了一个部分,他们只是想让你复制和粘贴一个代码,让NASA的RSS提要和android应用程序相互通信,但出于某种原因,什么也没有显示。我只是得到一个白色的屏幕和logcat错误似乎没有做太多的其他应用程序从我所看到的。我使用的测试数据有效,所以我认为xml很好,所以可能是我的Java。但我似乎什么也找不到。谁能看出我做错了什么。我整个周末都在努力。可能是
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,以备不时之需
<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>
AndroidManifest按要求提供
<?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>
编辑:添加了我的LogCat和我的AndroidManifest你的问题可能在你的构建中。gradle文件中,检查依赖项,你会看到如下内容:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
}
您可以尝试将其复制粘贴到依赖项上。在StackOverflow上发现一个问题,该问题与您遵循的教程相同。也许这对你有帮助
如前所述,您应该使用AsyncTask,并在请求返回时处理请求(onPostExecute)。代码的问题是,您发送了一个请求,在调用resetDisplay之后,响应尚未返回。。。这就是为什么你会看到一个空白屏幕。只是一个建议。这是开始学习android的一个很好的资源。您的清单有正确的权限吗?你能给他们看看吗?好的,我添加了我的logcat和android清单。抱歉,我忘了这些。谢谢你的链接Sojan V Jose。抱歉的副本可能需要处理一些工作。我当前的依赖项类似于以下“依赖项”{compilefiletree(dir:'libs',include:['*.jar'])compile'com.android.support:appcompat-v7:22.0.0'}“对不起,我不太明白如何很好地格式化它,我会在我的问题中添加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'
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
}