java Android setText导致崩溃
我正在尝试构建一个简单的小程序,下面有一个图像和一个文本视图。就像倒计时一样。我一直在尝试运行这个程序,所有的东西都可以运行,但当我运行到setText时,程序崩溃了。有什么问题吗?我做了很多搜索和更改,但仍然遇到同样的问题
package com.ob.IsIt;
import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.widget.ImageView;
import 安卓.widget.TextView;
public class Isit1337 extends Activity {
ImageView image;
TextView t;
String until;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
until = "fnkar inte";
t=(TextView)findViewById(R.layout.main);
run();
}
private void run()
{
if(IsIt.check())
{
image.setEnabled(false);
}
until = IsIt.timeLeft();
t.setText("test");
}
}
IsIt有效,IsIt有效。检查返回一个布尔值和IsIt。timeLeft返回一个字符串。 xml代码如下所示
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:gravity="bottom|center|center_vertical|top"
安卓:orientation="vertical" >
<ImageView
安卓:id="@+id/image"
安卓:layout_width="277dp"
安卓:layout_height="441dp"
安卓:src="@drawable/blackno" />
<TextView
安卓:id="@+id/t"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_weight="0.46"
安卓:textAppearance="?安卓:attr/textAppearanceLarge" />
</LinearLayout>
提前谢谢你
# 1 楼答案
因为R.id.main是你的布局
应该是:
# 2 楼答案
你必须写:
查找
TextView
的ID# 3 楼答案
查看以下文本视图:
# 4 楼答案
换成这个
# 5 楼答案
我猜你得到了一个NullPointerException。(在询问此类问题时,最好同时粘贴相关的LogCat输出。)更改此行:
为此:
# 6 楼答案