尝试开始Android开发,并使用TextViews做一些基本的工作..
由于某些原因,TextView的setText()方法给我带来了巨大的问题..这是我的代码的简化版本,以显示我的意思:
package com.example.testapp;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
text = (TextView) findViewById(R.id.text1);
setContentView(R.layout.activity_main);
text.setText("literally anything");
}
}
Run Code Online (Sandbox Code Playgroud)
这会导致崩溃,我不明白为什么..如果我在onCreate中创建TextView它工作正常,但如果我在它之外创建它,它不会..为什么会这样?有"TextView文字"这一行吗?尚未执行或什么?
谢谢!
我正在尝试为Android创建一个程序,该程序将以文本形式读取,然后打印出来,jsut以便习惯使用Android的I/O. 但是,每当我在AVD上运行它时,它会在LogCat中返回一个错误,说我的代码中存在NullPointerException,因此它"无法实例化ComponentInfo活动".
我找不到我指的是什么零.
这是我的代码:
import android.app.*;
import android.os.*;
import android.view.View;
import android.view.View.*;
import android.widget.*;
public class ButtonTestActivity extends Activity {
TextView tv = (TextView) findViewById(R.id.textView1);
EditText et = (EditText) findViewById(R.id.edittext);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.Enter);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
tv.setText(et.getText().toString());
}
});
}
Run Code Online (Sandbox Code Playgroud)
}
这是我的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" …Run Code Online (Sandbox Code Playgroud) 在Java for Android中,我想创建一个每秒增加1的变量,换句话说,它是重要的,这样我可以检查是否在过去的3秒内调用了一个函数,如果没有,我想要它做了不同的事情.
有没有内置的方法来做到这一点?我熟悉Timer类,但它似乎没有像我想要的那样工作..还有什么吗?
tl; dr:我想创建一个每秒增加1的变量,所以我可以用它来根据自上次调用以来的时间长短来对待函数.是否有捷径可寻?如果没有,这样做有什么困难?