Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
simon123123
1,291 PointsI have an error
package com.example.socialneworkshortcut;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button post = (Button)findViewById(R.id.button1);
Button buttonimage = (Button)findViewById(R.id.button2);
final EditText text = (EditText)findViewById(R.id.editText1);
ImageView image = (ImageView)findViewById(R.id.imageView1);
buttonimage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Where do you want your photo from?"), 1);
}
});
public void onActivityResult(int reqCode, int resCode, Intent data) {
if(resCode == RESULT_OK) {
if(reqCode == 1)
image.setImageURI(data.getData());
}
}
post.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, text.getText().toString());
startActivity(Intent.createChooser(shareIntent, "Where do you want to share?"));
}
});
}
}
public void onActivityResult(int reqCode, int resCode, Intent data) {
This line has an error that says "void is an invalid type for the variable onActivityResult"
I have no idea how to fix it could somebody help?
1 Answer
Ernest Grzybowski
Treehouse Project ReviewerYour problem exists because you are attempting to put onActivityResult() in your onCreate() method. This is a common mistake for most beginners. You definitely want to put the onActivityResult() method inside of your class (MainActivity), but just make sure that it is not within the curly braces of onCreate(). Once you do this, you'll get another error because you declared and initialized your variable image inside of onCreate() and then you are trying to use it in onActivityResult(). You can solve this by declaring your variables just inside of your class, but outside of any method. You can initialize them in onCreate(). The last thing is that you should also remember to put @Override just above your declaration of the onActivityResult() method. I moved your code around below, but don't mind any typos, but you should be good to go. Let me know if you're still having problems.
package com.example.socialneworkshortcut;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends Activity {
Button post, buttonimage;
EditText text;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
post = (Button) findViewById(R.id.button1);
buttonimage = (Button) findViewById(R.id.button2);
text = (EditText) findViewById(R.id.editText1);
image = (ImageView) findViewById(R.id.imageView1);
buttonimage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Where do you want your photo from?"), 1);
}
});
post.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, text.getText()
.toString());
startActivity(Intent.createChooser(shareIntent,
"Where do you want to share?"));
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
image.setImageURI(data.getData());
}
}
}
}
simon123123
1,291 Pointssimon123123
1,291 PointsOh my goodness thank you so much!! :)