Im having this problem where my button wouldnt let me open the URL given. It simply force closes whenever I open this activity. Heres my code;
public class TemakiActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contentviewer);
ImageView imageView = (ImageView) findViewById(R.id.videolink);
imageView.setImageResource(R.drawable.videothumb);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.videolink);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
startActivity(browserIntent);
}
});
TextView helloTxt = (TextView)findViewById(R.id.ingredientslister);
helloTxt.setText(readTxt());
}
private String readTxt(){
InputStream inputStream = getResources().openRawResource(R.raw.temakiingredients);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1)
{
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return byteArrayOutputStream.toString();
}
}
When I open this activity it simply force closes! Problem seems to be with the button, I cant get it to work, any solutions?
you have same id for ImageView, and for Button. You must have different id’s or you have typed by mistake. The compiler gives error because of conflict of id’s.
you have ImageView findViewById(R.id.videolink)
Button findViewById(R.id.videolink);