开发者

"Force Close" Error on declaring TextView and ToggleButton

Well basicly I have a textview and when the application 开发者_如何转开发is created it sets a string as the textviews text not hard, but I get a force close error when I run the app on my phone.

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

Then I have a error on a togglebutton also

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

And I have errors for all my other buttons/textviews can anyone help, please?!

EDIT: I cant post pics because I am a new member, :( Link to imgshack http://imageshack.us/photo/my-images/849/unledggp.png/

If code for the whole textview snippet.

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 Function

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

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

}


Look for all instances of sd_textview and make sure the one that you're trying to reference is a TextView. If you want more clarity you can debug your code and see what object is actually being returned by not casting into 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);


Looking at your error log (assuming its the right error log) you have a ClassCastException in the checkWifi method. Edit your question and include ALL of the onCreate method and all of the checkWifi method, but I expect you are using the same id for multiple views.


Two things I can think of (although seeing more code would help).

Make sure you have called setContentView(R.layout.main) (or whatever your layout file is called). Do this BEFORE any attempt to use findViewById(...).

Secondly sdcard.setText(R.string.not_mounted); in this statement R.string.not_mounted is a resource ID (int) and not a string. You would need to use...

sdcard.setText(getString(R.string.not_mounted));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜