0

好吧,基本上我有一个 textview 并且在创建应用程序时它会将字符串设置为 textviews 文本并不难,但是当我在手机上运行应用程序时出现强制关闭错误。

TextView sdcard=(TextView)findViewById(R.id.sd_textview);
sdcard.setText(R.string.not_mounted);

然后我在切换按钮上也有错误

ToggleButton silent=(ToggleButton)findViewById(R.id.silentbutton);
silent.setChecked(false);

我的所有其他按钮/文本视图都有错误,有人可以帮忙吗?

编辑:我不能发布图片,因为我是新成员,:( 链接到 imgshack http://imageshack.us/photo/my-images/849/unledggp.png/

如果整个 textview 片段的代码。

if     (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_UNMOUNTED)) {

TextView sdcard=(TextView)findViewById(R.id.sd_textview);
sdcard.setText(R.string.not_mounted);

}

OnCreate 函数

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

checkSD();
checkRing();
checkWifi();
checkBt();

}
4

3 回答 3

1

查找 的所有实例sd_textview并确保您要引用的实例是TextView. 如果您想要更清晰,您可以调试代码并通过不强制转换来查看实际返回的对象 a TextView

View sdcard = findViewById(R.id.sd_textview); //debug this
//you can also log the View object to see the type 
Log.d("Test", "" + sdcard);
于 2011-05-29T18:00:00.613 回答
0

查看您的错误日志(假设它是正确的错误日志),您的checkWifi方法中有一个 ClassCastException 。编辑您的问题并包括所有onCreate方法和所有checkWifi方法,但我希望您对多个视图使用相同的 id。

于 2011-05-29T17:51:47.313 回答
0

我能想到两件事(尽管看到更多代码会有所帮助)。

确保您已调用setContentView(R.layout.main)(或调用任何您的布局文件)。在尝试使用findViewById(...).

其次sdcard.setText(R.string.not_mounted);,此语句R.string.not_mounted中是资源 ID (int) 而不是字符串。您将需要使用...

sdcard.setText(getString(R.string.not_mounted));
于 2011-05-29T17:54:38.033 回答