gpt4 book ai didi

java - 无法让 Android Tutorial 工作,初级程序员

转载 作者:行者123 更新时间:2023-11-30 11:05:09 25 4
gpt4 key购买 nike

我是 Android 编程的新手,总的来说我缺乏编程技能。我一直在关注 Head Start 教程,尽管我知道它在技术上已被删除以进行更新或其他内容。不管怎样,我到了他们只是希望您复制并粘贴代码以使 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>

任何帮助将不胜感激。我会问这里的人,但在我的国家“纳米比亚”,很少有人能接触到我能接触到的 android 编程。

我的 S4 设备上的 LogCat

    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 请求

    <?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>

构建.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'
}

编辑:添加了我的 LogCat 和我的 AndroidManifest

最佳答案

在 StackOverflow 上发现了一个问题,该问题遵循与您相同的教程。也许这对你有帮助

Android Head First "NASA daily image App"

如前所述,您应该使用 AsyncTask,并在请求返回时处理请求 (onPostExecute)。您的代码的问题是,您发送了一个请求,并且在您调用 resetDisplay 之后,响应还没有返回...这就是您看到空白屏幕的原因。

关于java - 无法让 Android Tutorial 工作,初级程序员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29744861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com