- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 java 和 android 的新手,而且我主要是自学成才。我需要帮助解决这个问题。这可能是一些非常愚蠢、明显或愚蠢的事情,但是嘿,我必须从某个地方开始,对吗?我的应用程序在启动时崩溃。尝试运行我的应用程序后,我在 logcat 中收到此消息。
这是我的代码,它是一个练习,我应该制作一个小应用程序,您可以在其中显示一些国家,然后输入他们的首都。我使用 3 个按钮、2 个 TextView 和 1 个编辑文本。
主 Activity .java
public class MainActivity extends Activity {
public int cuenta = 0;
public TextView questionlabel = (TextView) findViewById(R.id.textView1);
public TextView resultadolabel = (TextView) findViewById(R.id.textView2);
public Button startbutton = (Button) findViewById(R.id.button1);
public Button checkbutton = (Button) findViewById(R.id.button2);
public Button nextbutton = (Button) findViewById(R.id.button3);
public EditText inputbox = (EditText) findViewById(R.id.editText1);
public String[] mPaises = {
"Canada",
"Estados Unidos",
"Mexico",
"Guatemala",
"Belice"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//start button
questionlabel.setText(mPaises[cuenta]);
}});
checkbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// check button
if (cuenta == 0) {
if (inputbox.toString() == "Ottawa") {
resultadolabel.setText("Correcto");}
else {resultadolabel.setText("Respuesta correcta: Ottawa");}}
else if (cuenta == 1) {
if (inputbox.toString() == "Washington") {
resultadolabel.setText("Correcto");}
else {resultadolabel.setText("Respuesta correcta: Washington");}}
else if (cuenta == 2) {
if (inputbox.toString() == "Mexico") {
resultadolabel.setText("Correcto");}
else {resultadolabel.setText("Respuesta correcta: Mexico");}}
else if (cuenta == 3) {
if (inputbox.toString() == "Guatemala") {
resultadolabel.setText("Correcto");}
else {resultadolabel.setText("Respuesta correcta: Guatemala");}}
else if (cuenta == 4) {
if (inputbox.toString() == "Belmopan") {
resultadolabel.setText("Correcto");}
else {resultadolabel.setText("Respuesta correcta: Belmoopan");}}
}
});
nextbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// LO QUE HACE EL NEXT BUTTON
cuenta = cuenta + 1;
questionlabel.setText(mPaises[cuenta]);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
这是我的 Activity_Main
<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" >
<EditText
android:id="@+id/editText1"
android:inputType="text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="129dp"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/editText1"
android:layout_alignLeft="@+id/editText1"
android:layout_marginBottom="42dp"
android:layout_marginLeft="16dp"
android:text="@string/Start" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="63dp"
android:text="@string/pais" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:text="@string/check" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button2"
android:layout_toRightOf="@+id/button2"
android:text="@string/next" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_alignLeft="@+id/textView1"
android:layout_marginBottom="21dp"
android:text="@string/mensaje" />
还有我的 list
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.capitales.del.mundo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
最佳答案
您需要首先扩充您的布局,然后检索您的 UI 元素,否则 findViewById
返回 null
并因此导致您的应用崩溃此行的 NullPointerException
:
startbutton.setOnClickListener
setContentView(R.layout.activity_main);
扩展您的布局,然后获取您的 UI 元素。
public TextView questionlabel;
public TextView resultadolabel;
public Button startbutton;
public Button checkbutton;
public Button nextbutton;
public EditText inputbox;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
questionlabel = (TextView) findViewById(R.id.textView1);
resultadolabel = (TextView) findViewById(R.id.textView2);
startbutton = (Button) findViewById(R.id.button1);
checkbutton = (Button) findViewById(R.id.button2);
nextbutton = (Button) findViewById(R.id.button3);
inputbox = (EditText) findViewById(R.id.editText1);
/****/
}
注释:
if (inputbox.toString() == "Ottawa")
不要使用 ==
比较 Strings
的内容。请改用 equals()
。关于java - 打开跟踪文件时出错。没有那个文件或目录 [2],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20669392/
我正在为我的程序编写安装脚本,它应该在 Linux/Unix 操作系统上运行。以下文件的默认目录是什么: 可执行文件(程序)。程序应通过从命令行键入其名称来执行。 共享库。 第三方共享库(程序未开源,
我有一堆用户、组和应用程序注册,我的 MVC 应用程序使用 AAD 数据进行身份验证和授权。是否可以将 Azure Active Directory 从一个租户(目录)迁移到另一个租户(目录)?如果可
查看 cljsbuild 文档 https://github.com/emezeske/lein-cljsbuild :cljsbuild { :builds [{ ; The
忽略已经版本控制的文件 如果你不小心添加了一些应该被忽略的文件,你如何将它们从版本控制中去除而不会丢失它们?或许你有自己的IDE配置文件,不是项目的一部分,但将会花费很多时间使之按照自己的方式工作。
我想使用\tableofcontents 命令,但没有目录从新页面开始或在末尾创建新页面,并且所有内容都是单倍行距。我怎样才能做到这一点?我假设使用 tocloft,但有哪些选择? 谢谢 最佳答案 试
我有一些 javascript 菜单代码,可以在单独的目录中正常工作。但是,当我尝试从同一目录中调用相同的 .js 文件时,它不会看到这些文件。 以下内容来自另一个目录: script type="t
我有这样的路径: /my/path/to/important_folder 在同一级别上,我还有其他文件和文件夹想要在达到与 important_folder 相同的级别时列出。 我的文件夹可能更深,
1、获取文件路径实现 1.1 获取当前文件路径 ? 1
我正在使用最新版本的 NTEmacs。 我写了一个名为“.dir-locals.el”的文件,如下所示。 ((nil . ((tab-width . 8) (fill-column .
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
在我的 .vimrc 中有这些行 :set foldmethod=marker :set foldmarker=SECTION:,ENDSECTION: 用于自定义代码折叠。在我的文件中,相关语言的注
在 fish 中: for x in * echo $x end *这里包括所有目录和文件,如何只列出文件(或目录)? 最佳答案 fish 没有很多花哨的通配语法。但是,目录可以像这样迭代: f
这是我的目录结构: ├── src │ ├── helpers │ │ ├── __init__.py │ │ ├── foo.py │ │ └── bar.py │
我想递归重命名文件夹/目录名称并找到 this solution所以。但是这个命令没有效果 find . -type f -exec rename 's/old/new/' '{}' \; 这是一个正
我想在相册中创建一个文件夹,并希望将图像保存在创建的相册中。 这可能吗?有什么办法可以做到这一点吗? 我已经搜索过,大多数人都说这是不可能的。 感谢您的帮助。 最佳答案 您也许可以使用AssetsLi
如何在python中使用用户定义的名称创建临时文件/目录。我知道 tempfile .但是我看不到任何以文件名作为参数的函数。 注意:我需要这个来对包含临时文件的临时目录上的 glob(文件名模式匹配
我在项目中使用JaCoCo Gradle插件。 作为问题的一个示例,我的大部分代码都在com.me.mysoftware包下。 我正在使用代码生成器来生成build/generated/java/..
我正在尝试使用 Gradle 开始运行 jar 文件 我的任务如下所示: task startServer(type: Exec) { workingDir file("${buildDir}/a
如何在 Ant 中定义一个目录集,其中包括两个目录:项目的基目录和子目录“test”? 看起来您无法使用“/”、“.”或“”专门包含目录集的根目录。例如,这包括“./test”,但不包括“.”:
我正在使用 CTAGs 包,它使用 Sublime Text 2 生成两个文件 .tags 和 .tags_sorted_by_file。 那么当我进行项目搜索(CMD + SHIFT + F)时,如
我是一名优秀的程序员,十分优秀!