EDIT:
just incase you read this i got the problem sorted i had gotten confused by my layouts and was editing the wrong one when i realised this all was sorted!
END EDIT!
every time i add a thread to my application it causes it to crash when the activity containing the thread is called below is the code piece containing the thread:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cam);
Toast t = Toast.makeText(this, "Just Click The Magnifying Glass To Search", 5000); //creates a new pop up message that lasts for 5 seconds
t.setGravity(Gravity.CENTER|Gravity.CENTER, 0, 0);
t.show()开发者_如何学JAVA;
bt = (ImageButton)findViewById(R.id.button); //creates instance of button
bt.setOnClickListener(search); //starts an on click listener for button
preview = (SurfaceView)findViewById(R.id.myview); //creates instance of surfaceview
previewHolder = preview.getHolder(); //creates a surfaceholder
previewHolder.addCallback(this); //sets surfaceholder callback as the activity
previewHolder.setType(3); //sets the type to SURFACE_TYPE_PUSH_BUFFERS
th = new Thread() {
public void run() {
handler.post(new Runnable() {
public void run() {
tv.setVisibility(0);
}
});
}
};
th.start();
}
if anyone could shed some light on the situation it would be greatly appreciated
EDIT:
i have got the thread working and got it to call a method which creates Toast however when i try to modify a setting of the textview it throws an NullPointerException error
I don't see initialization of TextView tv
in your code (tv = (TextView)findViewById(R.id.id_of_textview);
).
May be it is the cause of the problem?
To show TextView
after 5 seconds, you can use handler.postDelayed()
If you're doing TextView tv = new TextView();
then this is wrong and even if you're doing TextView tv = (TextView)findViewById(R.id.tv);
then the layout will not be inflated before setContentView(); so you have to initialize tv in onCreate() method.
精彩评论