Reputation: 35
I am trying to make an android studio program that searches twitter. I am using Twitter4J to write the code. I thought I had the code correct but right now the app crashes as soon as it loads. Here is the code.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
public class MainActivity extends AppCompatActivity {
TextView tweetDisplay =(TextView) findViewById(R.id.tweettext);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button Buffalo = (Button) findViewById(R.id.BuffaloButton);
Buffalo.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("Mykey")
.setOAuthConsumerSecret("my secret")
.setOAuthAccessToken("token")
.setOAuthAccessTokenSecret("my token sercert");
Twitter twitter = TwitterFactory.getSingleton();
try {
Query query = new Query("#Buffaloriver");
QueryResult result;
result = twitter.search(query);
for (Status status : result.getTweets()) {
tweetDisplay.setText("@" +
status.getUser().getScreenName() + ":" + status.getText());
}
}
catch(TwitterException e){
tweetDisplay.setText(R.string.Oops);
e.printStackTrace();
}
}
});
}
};
I have all my keys added in, and I have the internet access turned on in the manifest. I do not know what I am doing wrong with this. Thanks for the help!
Upvotes: 0
Views: 217
Reputation: 1847
You initialized the tweetDisplay object at wrong place. It should be under onCreate methode after setContentview called
Upvotes: 1