在经典在线教程中找到合适的文件来构建Android项目(Finding the Right Files to Build Android Project in Classic Online Tutor

编程入门 行业动态 更新时间:2024-10-25 17:27:49
在经典在线教程中找到合适的文件来构建Android项目(Finding the Right Files to Build Android Project in Classic Online Tutorial)

我在本教程的第7周遇到问题, https://web.stanford.edu/class/cs193a/lectures.shtml 。 在第7周,只有.java文件显示在Android Studio上运行构建时出现问题。

我是否需要xml文件? 为什么它们不会在前几周显示xml文件也显示出来。

我打开了Targets应用程序的zip文件,并在Targets / app / src / main / res / layout中找到了以下xml文件,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".TargetsActivity"> <com.example.stepp.targets.TargetsView android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>

但是当我将它复制到我粘贴.java文件的项目时,ExampleView.java和TargetsView.java,

/* * CS 193A, Winter 2015, Marty Stepp * This class is a graphical view of a basic example of 2D graphics. */ package com.example.stepp.targets; import android.content.Context; import android.graphics.*; import android.util.AttributeSet; import android.view.View; public class ExampleView extends View { public ExampleView(Context context, AttributeSet attrs) { super(context, attrs); } /* * This method draws some shapes and text on the view. */ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawARGB(255, 255, 90, 90); Paint aqua = new Paint(); aqua.setARGB(255, 0, 80, 220); canvas.drawRect(new RectF(10, 30, 300, 700), aqua); canvas.drawOval(new RectF(400, 50, getWidth(), getHeight()), aqua); Paint font = new Paint(); font.setARGB(255, 0, 0, 0); font.setTypeface(Typeface.create(Typeface.SERIF, Typeface.BOLD_ITALIC)); font.setTextSize(40); canvas.drawText("CS 193A is great", 80, 200, font); } }

/* * CS 193A, Winter 2015, Marty Stepp * This class is a graphical view of a drawing of a red/white target figure. */ package com.example.stepp.targets; import android.content.Context; import android.graphics.*; import android.util.AttributeSet; import android.view.View; public class TargetsView extends View { public TargetsView(Context context, AttributeSet attrs) { super(context, attrs); } /* * This method draws the target oval shapes on the view. */ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint red = new Paint(); red.setARGB(255, 255, 0, 0); Paint white = new Paint(); white.setARGB(255, 255, 255, 255); int w = getWidth(); int h = getHeight(); for (int i = 0; i < 5; i++) { canvas.drawOval(new RectF(w*i/10, h*i/10, w*(10-i)/10, h*(10-i)/10), (i % 2 == 0 ? red : white)); } } }

我不明白为什么这不起作用,因为同样的方法在早期工作。 非常感谢帮助。 我也无法理解为什么需要两个不同的活动ExampleView和TargetsView。

我在本教程的后几周遇到了同样的问题,因为我看不到在哪里找到正确的java和xml文件。

这个问题形成了对于那些关于Android应用程序开发的这个着名教程的人们的常见错误

I am having problems with Week 7 of this tutorial, https://web.stanford.edu/class/cs193a/lectures.shtml. On Week 7, There is only the .java files showing which was problematic when I ran the build on Android studio.

Do I need an xml file or not? Why could they not have been shown as in the earlier weeks the xml files were shown as well.

I opened the zip file for the Targets app and found in Targets/app/src/main/res/layout the following xml file,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".TargetsActivity"> <com.example.stepp.targets.TargetsView android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>

but when I copy it into the project where I pasted the .java files, ExampleView.java and TargetsView.java,

/* * CS 193A, Winter 2015, Marty Stepp * This class is a graphical view of a basic example of 2D graphics. */ package com.example.stepp.targets; import android.content.Context; import android.graphics.*; import android.util.AttributeSet; import android.view.View; public class ExampleView extends View { public ExampleView(Context context, AttributeSet attrs) { super(context, attrs); } /* * This method draws some shapes and text on the view. */ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawARGB(255, 255, 90, 90); Paint aqua = new Paint(); aqua.setARGB(255, 0, 80, 220); canvas.drawRect(new RectF(10, 30, 300, 700), aqua); canvas.drawOval(new RectF(400, 50, getWidth(), getHeight()), aqua); Paint font = new Paint(); font.setARGB(255, 0, 0, 0); font.setTypeface(Typeface.create(Typeface.SERIF, Typeface.BOLD_ITALIC)); font.setTextSize(40); canvas.drawText("CS 193A is great", 80, 200, font); } }

and

/* * CS 193A, Winter 2015, Marty Stepp * This class is a graphical view of a drawing of a red/white target figure. */ package com.example.stepp.targets; import android.content.Context; import android.graphics.*; import android.util.AttributeSet; import android.view.View; public class TargetsView extends View { public TargetsView(Context context, AttributeSet attrs) { super(context, attrs); } /* * This method draws the target oval shapes on the view. */ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint red = new Paint(); red.setARGB(255, 255, 0, 0); Paint white = new Paint(); white.setARGB(255, 255, 255, 255); int w = getWidth(); int h = getHeight(); for (int i = 0; i < 5; i++) { canvas.drawOval(new RectF(w*i/10, h*i/10, w*(10-i)/10, h*(10-i)/10), (i % 2 == 0 ? red : white)); } } }

I dont see why this doesnt work because the same method working in the early weeks. Help is much appreciated. Nor can I see why there is a need for two different activities ExampleView and TargetsView.

I am having the same problem in the other latter weeks of this tutorial as I cannot see where to find the correct java and xml files.

This problems forms what seems to be a common error for those following this prominent tutorials on android app development

最满意答案

尝试使用整个Targets.zip。

您在问题中包含的两个类是类extends View 。 它们只是UI组件,不会让它们自己发生任何事情。

这两个类可以包含在布局xml中,也可以由Activity类使用(以编程方式构造)。 正如您在布局xml中看到的那样,它包含一个名为<com.example.stepp.targets.TargetsView ... />的标记,这意味着此布局内部将包含TargetView。

Activity类实际上是协调UI组件和逻辑的类。 在你的Targets.zip中,你也有一个TargetsActivity.java ,它是一个Acitivity类。 它尝试通过在onCreate()方法中调用setContentView(...)来获取R.layout.activity_targets并将其设置为内容视图。

最后你在src / main上有AndroidManifest.xml 。 您可以在<activity ...> ... </activity>节点中看到,TargetsActivity被指定为启动器活动,这意味着当应用程序启动时,此Acitivity类将是第一个启动的活动。

Try use the whole Targets.zip.

The two classes you included in the question are class extends View. They are just UI components that will not make anything happen by themselves.

These two classes can be included into a layout xml or used (constructed programmatically) by Activity class. As you can see in the layout xml you found, it includes a tag called <com.example.stepp.targets.TargetsView ... />, which means this layout will have a TargetView inside.

An Activity class is actually the one who coordinating UI components and logics. In your Targets.zip, you have a TargetsActivity.java as well, which is a Acitivity class. It tries to fetch R.layout.activity_targets and set it as content view by calling setContentView(...) in the onCreate() method.

Finally you have AndroidManifest.xml at src/main. You can see in the <activity ...> ... </activity> node, TargetsActivity is specified as the launcher activity, which means when the app starts, this Acitivity class will be the first activity to start.

更多推荐

本文发布于:2023-08-06 18:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1453872.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:合适   文件   项目   经典   中找到

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!