I am learning android programming using eclipse. I coded a twitter app that simply let's you tweet a status and view a user's timeline. However when I try run the app on my samsung galaxy tab 3.0 the app starts up, all i see is the title at the top, none of the textviews or buttons show up then after 2 seconds the screen goes black and i get the error message ("Unfortunately TwitterApp has stopped") I cant imagine it's something to do with twitter since none of the widgets are being displayed. Here is the code from my main activity, and the activity_main.xml file. And the logCat. Ill even include the manifest
package com.example.twitterapp;
import java.util.List;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView countCharsTV, timelineTV;
EditText usernameET, tweetET;
Button tweetBTN, timelineBTN;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countCharsTV = (TextView) findViewById(R.id.textView1);
timelineTV = (TextView) findViewById(R.id.textView2);
timelineTV.setMovementMethod(new ScrollingMovementMethod());
tweetET = (EditText) findViewById(R.id.editText1);
tweetET.addTextChangedListener(new MyTextWatcher());
usernameET = (EditText) findViewById(R.id.editText2);
tweetBTN = (Button) findViewById(R.id.button1);
tweetBTN.setOnClickListener(tweetBTNOnClickListener);
timelineBTN.setOnClickListener(timelineBTNOnClickListener);
}
//BUTTON CLICK LISTENERS WITHOUT IMPLEMENTS
public OnClickListener tweetBTNOnClickListener = new OnClickListener() {
@Override
public void onClick(View arg0) {
new MyAsyncTaskTweet().execute(tweetET.getText().toString());
}
};
public OnClickListener timelineBTNOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
new MyAsyncTaskTimeline().execute(timelineTV.getText().toString());
}
};
//END OF BUTTON CLICK LISTENERS
//COUNT CHARS IN TEXTVIEW USING IMPLEMENTS
class MyTextWatcher implements TextWatcher {
@Override
public void afterTextChanged(Editable arg0) {
countCharsTV.setText("" + tweetET.getText().length());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
}
//End OF COUNT CHARS IN TEXTVIEW
//ASYNC TASK CLASSES
//TWEET ASYNC TASK
public class MyAsyncTaskTweet extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... tweet) {
String result = "";
Twitter twitter = TwitterFactory.getSingleton();
try{
twitter.updateStatus(tweet[0]);
result = "Success";
}catch(TwitterException twitterException) {
result = "Failed to update status";
}
return result;
}
@Override
protected void onPostExecute(String result){
tweetET.setHint(result);
tweetET.setText("");
}
}
//END OF TWEET ASYNC TASK
public class MyAsyncTaskTimeline extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... username) {
String result = new String("");
List<twitter4j.Status> statuses = null;
Twitter twitter = TwitterFactory.getSingleton();
try {
statuses = twitter.getUserTimeline(username[0]);
}catch (TwitterException twitterException) {
twitterException.printStackTrace();
}
for(twitter4j.Status status : statuses) {
result += status.getText();
result += "\n";
}
return result;
}
@Override
protected void onPostExecute(String result) {
usernameET.setText("");
timelineTV.setText(result);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:text="Tweet" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="Type your tweet here" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_toLeftOf="@+id/editText2"
android:text="'@" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_toRightOf="@+id/textView1"
android:text="Timeline" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:ems="10"
android:hint="Type a username here" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_toRightOf="@+id/button1"
android:text="Timeline will go here" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.twitterapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.twitterapp.MainActivity"
android:label="@string/app_name" >
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Logcat
03-21 22:46:25.117: W/ActivityThread(3806): Application com.example.twitterapp can be
debugged on port 8100...
03-21 22:46:25.289: E/SensorManager(3806): thread start
03-21 22:46:25.296: D/SensorManager(3806): registerListener :: handle = 1598182229
name= BOSCH BMC150 Acceleration Sensor delay= 200000
03-21 22:46:25.296: D/AndroidRuntime(3806): Shutting down VM
03-21 22:46:25.296: W/dalvikvm(3806): threadid=1: thread exiting with uncaught exception
(group=0x40dcd2a0)
03-21 22:46:25.304: E/AndroidRuntime(3806): FATAL EXCEPTION: main
03-21 22:46:25.304: E/AndroidRuntime(3806): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.example.twitterapp/com.example.twitterapp.MainActivity}:
java.lang.NullPointerException
03-21 22:46:25.304: E/AndroidRuntime(3806): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2129)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2154)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
android.app.ActivityThread.access$700(ActivityThread.java:146)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1260)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
android.os.Handler.dispatchMessage(Handler.java:99)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
android.os.Looper.loop(Looper.java:137)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
android.app.ActivityThread.main(ActivityThread.java:4949)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
java.lang.reflect.Method.invokeNative(Native Method)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
java.lang.reflect.Method.invoke(Method.java:511)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
dalvik.system.NativeStart.main(Native Method)
03-21 22:46:25.304: E/AndroidRuntime(3806): Caused by: java.lang.NullPointerException
03-21 22:46:25.304: E/AndroidRuntime(3806): at
com.example.twitterapp.MainActivity.onCreate(MainActivity.java:45)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
android.app.Activity.performCreate(Activity.java:5185)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-21 22:46:25.304: E/AndroidRuntime(3806): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
03-21 22:46:25.304: E/AndroidRuntime(3806): ... 11 more
03-21 22:46:38.398: I/Process(3806): Sending signal. PID: 3806 SIG: 9
Where is timelineBTN reference? "timelineBTN" was null. timelineBTN = (Button)findViewById(R.id.);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.