问题描述
在通过 youtube 学习如何更改布局,如何在 MainActivity 中放置 Buttons、EditText、TextViews 之后,我尝试了启动新 Activity 的最简单方法。 当我运行活动时,我看到带有文本“打开活动 2”的按钮。我可以点击它。并且......没有任何反应。 我应该能够使用 TextView = "Activity 2" 查看 activity_second 的布局。 相反,我可以一直点击按钮但看不到“活动 2” 在这个网站我了解到问题可能是意图,所以我改变了: Intent intent = new Intent(this,SecondActivity.class) by Intent intent = new Intent (MainActivity.this,SecondActivity.class) 我什至想过使用getBaseContext 代替,但尚未完成。 我相信问题可能是我们需要在 SecondActivity.java 中以某种方式调用 TextView 理论上,调用布局activity_second.xml应该就够了,因为会出现android:text="Activity 2" 另一个可疑之处是我没有使用 Android Studio,也没有使用 AppCompatActivity,而是从使用它们的页面复制我的代码。相反,我只使用 MainActivity extends Activity 也许我正在监督诸如投射小部件或类似的事情。 我的问题是你看到某种类型。 我已经克服了一些“找不到符号”的问题,例如我的 id 中的拼写错误,编译器没有找到或忘记导入一些小部件或使用逗号而不是点。 我使用 emacs 作为编辑器并使用这个慷慨的博客编译代码。我已经安装了 openjdk、javac 和基本的 android 工具 我最大程度地简化了代码,将其用作基础 @L_502_0@ 和流程中的频道代码 YouTube。 这是我的代码,错字在哪里? p.S:这是我的第一篇文章,代码格式似乎不正确,希望您能够阅读。
MainActivity.java
package net.otro.abrir1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.util.Log;
import android.widget.TextView;
import android.widget.Button;
public class MainActivity extends Activity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setonClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
openActivity2();
}
});
}
/** Called when the user taps the Send button */
public void openActivity2(){
// Do something in response to button
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
}
SecondActivity.java
package net.otro.abrir1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.view.View;
import android.widget.EditText;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="open activity 2"
android:id="@+id/button"/>
</RelativeLayout>
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview"
android:text="Activity 2"/>
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="net.otro.abrir1"
versionCode="1"
versionName="0.1">
<uses-sdk android:minSdkVersion="16"/>
<application android:label="Abrir1">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".SecondActivity"
android:parentActivityName=".MainActivity">
<!--android:label="@string/activity2_name" -->
<Meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>
</manifest>
解决方法
问题是:错字在哪里。答案是没有错别字,但我试图用这个命令编译:
javac -source 1.7 -target 1.7 -bootclasspath "${JAVA_HOME}"/jre/lib/rt.j$ \
-classpath "${PLATFORM}/android.jar" -d build/obj \
build/gen/com/probando/intento1/R.java \
"${PROJ}"/src/com/probando/intento1/MainActivity.java
本来的样子
javac -source 1.7 -target 1.7 -bootclasspath "${JAVA_HOME}"/jre/lib/rt.j$ \
-classpath "${PLATFORM}/android.jar" -d build/obj \
build/gen/com/probando/intento1/R.java \
"${PROJ}"/src/com/probando/intento1/*.java
我原以为 MainActivity.java 的编译足以调用 SecondActivity.java,但事实并非如此。 它必须首先编译成“R” R.java 是一个文件,其信息与您的文件 MainActivity.java 和 SecondActivity 相同。 java,但是可以看到内存地址,而不是变量。
Comment1:标题反映了我怀疑 AppCompatActivity 可能是根本原因,但事实并非如此。 AppCompatActivity 只是更高版本的 Android 中包含的一个设置。为了使用它,您需要在 java 文件标题的导入部分中指定它 评论 2:我推荐这个网站:https://www.hanshq.net/command-line-android.html 避免使用Android Studio。 只需 3 个文件和几行代码,您就拥有了您的第一个应用程序。稍后,您只需要修改它。 评论 3:以上指南适用于 64 位和 25.0.0 版。如果您碰巧有 32 位和 19.0.0 版本,那么做一个小改动可能会有所帮助: 对于 64 位:
$ "${BUILD_TOOLS}/zipalign" -f -p 4 \
build/Hello.unsigned.apk build/Hello.aligned.apk
对于 32 位:
$ "${BUILD_TOOLS}/zipalign" -f 4 \
build/Hello.unsigned.apk build/Hello.aligned.apk
出于某种原因,Android 在更高版本中停止支持 32 位。
您需要删除标志-p,由于某种原因无法识别该标志。
评论 4:感谢上述网站的作者 Hans 为那些出于任何原因不想使用 Android Studio 的人提供的精彩指南 就我而言,Android Studio 在 4 RAM 的 Windows 机器上运行速度如此之慢,以至于我不敢将其安装在具有 2.5 RAM 的 Centos 7 中。
对于那些负担不起的人来说,命令行确实是 Android Studio 的一个选项。