I am getting the following error in my logcat:
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: FATAL EXCEPTION: main
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: Process: in.aekansh.cortibae, PID: 27708
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: java.lang.ExceptionInInitializerError
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at in.aekansh.cortibae.MainActivity.onCreate(MainActivity.java:130)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5990)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2310)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2419)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at android.app.ActivityThread.access$900(ActivityThread.java:154)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5293)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources in.aekansh.cortibae.App.getResources()' on a null object reference
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at in.aekansh.cortibae.AndroidUtilities.<clinit>(AndroidUtilities.java:32)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at in.aekansh.cortibae.MainActivity.onCreate(MainActivity.java:130)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5990)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2310)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2419)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at android.app.ActivityThread.access$900(ActivityThread.java:154)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5293)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
03-28 15:07:12.758 27708-27708/in.aekansh.cortibae E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
03-28 15:07:14.863 27708-27708/in.aekansh.cortibae I/Process: Sending signal. PID: 27708 SIG: 9
I don't know how to remove the error.
This is my MainActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AndroidUtilities.statusBarHeight = getStatusBarHeight();
chatMessages = new ArrayList<>();
chatListView = (ListView) findViewById(R.id.chat_list_view);
chatEditText1 = (EditText) findViewById(R.id.chat_edit_text1);
enterChatView1 = (ImageView) findViewById(R.id.enter_chat1);
// Hide the emoji on click of edit text
chatEditText1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (showingEmoji)
hideEmojiPopup();
}
});
emojiButton = (ImageView)findViewById(R.id.emojiButton);
emojiButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showEmojiPopup(!showingEmoji);
}
});
listAdapter = new ChatListAdapter(chatMessages, this); //load the messages from the array.
chatListView.setAdapter(listAdapter);
chatEditText1.setOnKeyListener(keyListener);
enterChatView1.setOnClickListener(clickListener);
chatEditText1.addTextChangedListener(watcher1);
sizeNotifierRelativeLayout = (SizeNotifierRelativeLayout) findViewById(R.id.chat_layout);
sizeNotifierRelativeLayout.delegate = this;
NotificationCenter.getInstance().addObserver(this, NotificationCenter.emojiDidLoaded);
}
AndroidUtilities.java:
public class AndroidUtilities {
public static float density = 1;
public static int statusBarHeight = 0;
public static Point displaySize = new Point();
static {
density = App.getInstance().getResources().getDisplayMetrics().density;
checkDisplaySize();
}
public static int dp(float value) {
return (int)Math.ceil(density * value);
}
public static void runOnUIThread(Runnable runnable) {
runOnUIThread(runnable, 0);
}
public static void runOnUIThread(Runnable runnable, long delay) {
if (delay == 0) {
App.applicationHandler.post(runnable);
} else {
App.applicationHandler.postDelayed(runnable, delay);
}
}
public native static void loadBitmap(String path, Bitmap bitmap, int scale, int width, int height, int stride);
public static boolean copyFile(InputStream sourceFile, File destFile) throws IOException {
OutputStream out = new FileOutputStream(destFile);
byte[] buf = new byte[4096];
int len;
while ((len = sourceFile.read(buf)) > 0) {
Thread.yield();
out.write(buf, 0, len);
}
out.close();
return true;
}
public static boolean copyFile(File sourceFile, File destFile) throws IOException {
if(!destFile.exists()) {
destFile.createNewFile();
}
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
} catch (Exception e) {
//FileLog.e("tmessages", e);
return false;
} finally {
if(source != null) {
source.close();
}
if(destination != null) {
destination.close();
}
}
return true;
}
public static void checkDisplaySize() {
try {
WindowManager manager = (WindowManager) App.getInstance().getSystemService(Context.WINDOW_SERVICE);
if (manager != null) {
Display display = manager.getDefaultDisplay();
if (display != null) {
if (android.os.Build.VERSION.SDK_INT < 13) {
displaySize.set(display.getWidth(), display.getHeight());
} else {
display.getSize(displaySize);
}
}
}
} catch (Exception e) {
}
}
public static int getViewInset(View view) {
if (view == null || Build.VERSION.SDK_INT < 21) {
return 0;
}
try {
Field mAttachInfoField = View.class.getDeclaredField("mAttachInfo");
mAttachInfoField.setAccessible(true);
Object mAttachInfo = mAttachInfoField.get(view);
if (mAttachInfo != null) {
Field mStableInsetsField = mAttachInfo.getClass().getDeclaredField("mStableInsets");
mStableInsetsField.setAccessible(true);
Rect insets = (Rect)mStableInsetsField.get(mAttachInfo);
return insets.bottom;
}
} catch (Exception e) {
// FileLog.e("tmessages", e);
}
return 0;
}
}
Please tell me where am I getting the NullPointerException. I think I have called all the necessary functions after super.onCreate(); so I don't think there should be any problem. Please help me out. Ask me for more information if you need it.
******* UPDATE *******
App.java:
public class App extends Application {
private static App Instance;
public static volatile Handler applicationHandler = null;
#Override
public void onCreate() {
super.onCreate();
Instance=this;
applicationHandler = new Handler(getInstance().getMainLooper());
NativeLoader.initNativeLibs(App.getInstance());
}
public static App getInstance()
{
return Instance;
}
}
Your App.getInstance() is returning null. The issue here is static block code at AndroidUtilities.java:
static {
density = App.getInstance().getResources().getDisplayMetrics().density;
checkDisplaySize();
}
Static initialization blocks are run when the JVM (class loader - to be specific) loads StaticClass (which occurs the first time it is referenced in code) and before your application started (before onCreate() method of App class was invoked), you can read more at here and here.
The solution is remove above static code block and create a static method at AndroidUtilities.java
public class AndroidUtilities {
public static float density = 1;
public static int statusBarHeight = 0;
public static Point displaySize = new Point();
public static void init() {
density = App.getInstance().getResources().getDisplayMetrics().density;
checkDisplaySize();
}
// your other code
}
App.java:
public class App extends Application {
public void onCreate() {
super.onCreate()
Instance = this;
AndroidUtilities.init();
// your other code
}
}
Android Manifests check application, add android:name=".App" and android:allowBackup="true" and permission
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"></uses-permission>
<application
android:name=".App"
android:allowBackup="true"
android:supportsRtl="true"
And activity add windowSoftInputMode attribute
<activity
android:name=".UserChatActivity"
android:windowSoftInputMode="adjustResize"></activity>
Grandle Control useLibrary and sourceSets.main
useLibrary 'org.apache.http.legacy'
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets.main {
jniLibs.srcDir 'libs'
jni.srcDirs = [] //disable automatic ndk-build call
}
Rebuid.
Related
It seems that it just created AppComponent and then delete it. In other my projects it's works correctly. Why appComponent become null and how to make it working ?
App.class
public class App extends Application {
private static AppComponent appComponent;
#Override
public void onCreate() {
super.onCreate();
buildGraphAndInject();
}
public static AppComponent getAppComponent() {
if(appComponent == null) {
Log.d("getter", "null");
}
return appComponent;
}
public void buildGraphAndInject() {
appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
appComponent.inject(this);
if(appComponent == null) {
Log.d("build", "null");
}
}
}
call from Activity
#Override
public void setupComponent(AppComponent appComponent) {
mComponent = DaggerMainComponent.builder()
.appComponent(App.getAppComponent())
.build();
mComponent.inject(this);
}
AppComponent
#Component(modules = {AppModule.class})
public interface AppComponent {
void inject(App app);
}
result
04-02 02:16:23.544 32016-32016/exp.privatebank D/getter: null
04-02 02:16:23.569 32016-32016/exp.privatebank E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{exp.privatebank/exp.privatebank.view.activity.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at dagger.internal.Preconditions.checkNotNull(Preconditions.java:32)
at exp.privatebank.di.DaggerMainComponent$Builder.appComponent(DaggerMainComponent.java:110)
at exp.privatebank.view.activity.MainActivity.setupComponent(MainActivity.java:58)
at exp.privatebank.common.BaseActivity.onCreate(BaseActivity.java:19)
at exp.privatebank.view.activity.MainActivity.onCreate(MainActivity.java:38)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
I am trying to send my web service response from one activity to another .when I move one activity to another activity without response it work fine .but when I send response it give me error .could you please help me removing this exception ..
Actually I am sending like this:I think problem in this line i.putExtra("test", data);
I do like this..
10-25 21:13:47.619: E/AndroidRuntime(962): FATAL EXCEPTION: main
10-25 21:13:47.619: E/AndroidRuntime(962): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.firstgroup.applicationload/com.firstgroup.ui.screens.Departuredashboardscreen}: java.lang.NullPointerException
10-25 21:13:47.619: E/AndroidRuntime(962): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.os.Handler.dispatchMessage(Handler.java:99)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.os.Looper.loop(Looper.java:137)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-25 21:13:47.619: E/AndroidRuntime(962): at java.lang.reflect.Method.invokeNative(Native Method)
10-25 21:13:47.619: E/AndroidRuntime(962): at java.lang.reflect.Method.invoke(Method.java:511)
10-25 21:13:47.619: E/AndroidRuntime(962): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-25 21:13:47.619: E/AndroidRuntime(962): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-25 21:13:47.619: E/AndroidRuntime(962): at dalvik.system.NativeStart.main(Native Method)
10-25 21:13:47.619: E/AndroidRuntime(962): Caused by: java.lang.NullPointerException
10-25 21:13:47.619: E/AndroidRuntime(962): at android.os.Parcel.readListInternal(Parcel.java:2237)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.os.Parcel.readList(Parcel.java:1531)
10-25 21:13:47.619: E/AndroidRuntime(962): at com.firstgroup.dto.deparaturedaseboarddto.<init>(deparaturedaseboarddto.java:209)
10-25 21:13:47.619: E/AndroidRuntime(962): at com.firstgroup.dto.deparaturedaseboarddto.<init>(deparaturedaseboarddto.java:193)
10-25 21:13:47.619: E/AndroidRuntime(962): at com.firstgroup.dto.deparaturedaseboarddto$1.createFromParcel(deparaturedaseboarddto.java:185)
10-25 21:13:47.619: E/AndroidRuntime(962): at com.firstgroup.dto.deparaturedaseboarddto$1.createFromParcel(deparaturedaseboarddto.java:1)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.os.Parcel.readParcelable(Parcel.java:2103)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.os.Parcel.readValue(Parcel.java:1965)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.os.Parcel.readMapInternal(Parcel.java:2226)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.os.Bundle.unparcel(Bundle.java:223)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.os.Bundle.getParcelable(Bundle.java:1173)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.content.Intent.getParcelableExtra(Intent.java:4330)
10-25 21:13:47.619: E/AndroidRuntime(962): at com.firstgroup.ui.screens.Departuredashboardscreen.onCreate(Departuredashboardscreen.java:21)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.app.Activity.performCreate(Activity.java:5104)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-25 21:13:47.619: E/AndroidRuntime(962): ... 11 more
#Override
public void getWebserviceResponse(String result) {
// TODO Auto-generated method stub
//JSONObject js=new Jso(result);
deparaturedaseboarddto data = new Gson().fromJson(result, deparaturedaseboarddto.class);
/*listView=(ListView)findViewById(R.id.listView1);
adapter = new CustomListAdapter(this, data.getData());
listView.setAdapter(adapter);*/
Intent i = new Intent(Appliacationload.this,Departuredashboardscreen.class);
i.putExtra("test", data);
startActivity(i);
finish();
Log.d("----", "========");
}
Second Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.departure_dashboard);
Intent i = getIntent();
deparaturedaseboarddto myParcelableObject = (deparaturedaseboarddto) i.getParcelableExtra("test");
Log.d("===", "hhhh");
}
**Model class :**
public class deparaturedaseboarddto implements Parcelable{
ArrayList<deparaturedaseboarddto> data;
public ArrayList<deparaturedaseboarddto> getData() {
return data;
}
public void setData(ArrayList<deparaturedaseboarddto> data) {
this.data = data;
}
#SerializedName("alertsId")
int alertsId;
#SerializedName("destExpArrival")
String destExpArrival;
#SerializedName("destSchArrival")
String destSchArrival;
#SerializedName("expDepart")
String expDepart;
#SerializedName("filteredStation")
String filteredStation;
#SerializedName("platformNo")
String platformNo;
#SerializedName("rid")
String rid;
#SerializedName("schDepart")
String schDepart;
#SerializedName("toc")
String toc;
#SerializedName("toExpArrival")
String toExpArrival;
#SerializedName("toSchArrival")
String toSchArrival;
#SerializedName("trainID")
String trainID;
#SerializedName("trainLastReportedAt")
String trainLastReportedAt;
#SerializedName("destinationStation")
DestinationStation destinationStation;
public deparaturedaseboarddto(String trainID,String toc,String trainLastReportedAt, String platformNo, String schDepart, String expDepart, int alertsId, String rid, String destSchArrival, String filteredStation, String destExpArrival, String toSchArrival, String toExpArrival,DestinationStation destinationStation){
super();
this.trainID=trainID;
this.toc=toc;
this.trainLastReportedAt=trainLastReportedAt;
this.platformNo=platformNo;
this.schDepart=schDepart;
this.expDepart=expDepart;
this.alertsId=alertsId;
this.destinationStation=destinationStation;
this.toExpArrival=toExpArrival;
this.toSchArrival=toSchArrival;
this.destExpArrival=destExpArrival;
this.filteredStation=filteredStation;
this.destSchArrival=destSchArrival;
this.rid=rid;
}
public DestinationStation getDestinationStation() {
return destinationStation;
}
public void setDestinationStation(DestinationStation destinationStation) {
this.destinationStation = destinationStation;
}
public int getAlertsId() {
return alertsId;
}
public void setAlertsId(int alertsId) {
this.alertsId = alertsId;
}
public String getDestExpArrival() {
return destExpArrival;
}
public void setDestExpArrival(String destExpArrival) {
this.destExpArrival = destExpArrival;
}
public String getDestSchArrival() {
return destSchArrival;
}
public void setDestSchArrival(String destSchArrival) {
this.destSchArrival = destSchArrival;
}
public String getExpDepart() {
return expDepart;
}
public void setExpDepart(String expDepart) {
this.expDepart = expDepart;
}
public String getFilteredStation() {
return filteredStation;
}
public void setFilteredStation(String filteredStation) {
this.filteredStation = filteredStation;
}
public String getPlatformNo() {
return platformNo;
}
public void setPlatformNo(String platformNo) {
this.platformNo = platformNo;
}
public String getRid() {
return rid;
}
public void setRid(String rid) {
this.rid = rid;
}
public String getSchDepart() {
return schDepart;
}
public void setSchDepart(String schDepart) {
this.schDepart = schDepart;
}
public String getToc() {
return toc;
}
public void setToc(String toc) {
this.toc = toc;
}
public String getToExpArrival() {
return toExpArrival;
}
public void setToExpArrival(String toExpArrival) {
this.toExpArrival = toExpArrival;
}
public String getToSchArrival() {
return toSchArrival;
}
public void setToSchArrival(String toSchArrival) {
this.toSchArrival = toSchArrival;
}
public String getTrainID() {
return trainID;
}
public void setTrainID(String trainID) {
this.trainID = trainID;
}
public String getTrainLastReportedAt() {
return trainLastReportedAt;
}
public void setTrainLastReportedAt(String trainLastReportedAt) {
this.trainLastReportedAt = trainLastReportedAt;
}
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(alertsId);
dest.writeString(destExpArrival);
dest.writeString(destSchArrival);
dest.writeString(expDepart);
dest.writeString(filteredStation);
dest.writeString(platformNo);
dest.writeString(rid);
dest.writeString(schDepart);
dest.writeString(toc);
dest.writeString(toExpArrival);
dest.writeString(toSchArrival);
dest.writeString(trainID);
dest.writeString(trainLastReportedAt);
dest.writeParcelable(this.destinationStation, flags);
dest.writeList(data);
}
public static final Parcelable.Creator<deparaturedaseboarddto> CREATOR = new Parcelable.Creator<deparaturedaseboarddto>() {
public deparaturedaseboarddto createFromParcel(Parcel in) {
return new deparaturedaseboarddto(in);
}
public deparaturedaseboarddto[] newArray(int size) {
return new deparaturedaseboarddto[size];
}
};
private deparaturedaseboarddto(Parcel in) {
this.alertsId=in.readInt();
this.destExpArrival=in.readString();
this.destSchArrival=in.readString();
this.expDepart=in.readString();
this.filteredStation=in.readString();
this.platformNo=in.readString();
this.rid=in.readString();
this.schDepart=in.readString();
this.toc=in.readString();
this.toExpArrival=in.readString();
this.toSchArrival=in.readString();
this.trainID=in.readString();
this.trainLastReportedAt=in.readString();
this.destinationStation = in.readParcelable(DestinationStation.class.getClassLoader());
in.readList(data,deparaturedaseboarddto.class.getClassLoader());
}
}
package com.firstgroup.dto;
import android.os.Parcel;
import android.os.Parcelable;
import com.google.gson.annotations.SerializedName;
public class DestinationStation implements Parcelable {
#SerializedName("crsCode")
String crsCode;
#SerializedName("stationName")
String stationName;
public DestinationStation(String crsCode, String stationName) {
// TODO Auto-generated constructor stub
super();
this.crsCode=crsCode;
this.stationName=stationName;
}
public String getCrsCode() {
return crsCode;
}
public void setCrsCode(String crsCode) {
this.crsCode = crsCode;
}
public String getStationName() {
return stationName;
}
public void setStationName(String stationName) {
this.stationName = stationName;
}
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(crsCode);
dest.writeString(stationName);
}
public static final Parcelable.Creator<DestinationStation> CREATOR = new Parcelable.Creator<DestinationStation>() {
public DestinationStation createFromParcel(Parcel in) {
return new DestinationStation(in);
}
public DestinationStation[] newArray(int size) {
return new DestinationStation[size];
}
};
private DestinationStation(Parcel in) {
this.crsCode=in.readString();
this.stationName=in.readString();
}
}
Updated one..
#Override
public void getWebserviceResponse(String result) {
// TODO Auto-generated method stub
//JSONObject js=new Jso(result);
deparaturedaseboarddto data = new Gson().fromJson(result, deparaturedaseboarddto.class);
/*listView=(ListView)findViewById(R.id.listView1);
adapter = new CustomListAdapter(this, data.getData());
listView.setAdapter(adapter);*/
Intent i = new Intent(Appliacationload.this,Departuredashboardscreen.class);
EventBus.getDefault().postSticky(data);
//i.putExtra("test", (Parcelable)data);
startActivity(i);
finish();
Log.d("----", "========");
}
Next Activity:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.departure_dashboard);
Intent i = getIntent();
deparaturedaseboarddto myParcelableObject = (deparaturedaseboarddto) EventBus.getDefault().getStickyEvent(deparaturedaseboarddto.class);
Log.d("===", "hhhh");
}
Exception:
10-26 00:56:36.052: E/AndroidRuntime(806): FATAL EXCEPTION: AsyncTask #3
10-26 00:56:36.052: E/AndroidRuntime(806): java.lang.RuntimeException: An error occured while executing doInBackground()
10-26 00:56:36.052: E/AndroidRuntime(806): at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-26 00:56:36.052: E/AndroidRuntime(806): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
10-26 00:56:36.052: E/AndroidRuntime(806): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
10-26 00:56:36.052: E/AndroidRuntime(806): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
10-26 00:56:36.052: E/AndroidRuntime(806): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-26 00:56:36.052: E/AndroidRuntime(806): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-26 00:56:36.052: E/AndroidRuntime(806): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-26 00:56:36.052: E/AndroidRuntime(806): at java.lang.Thread.run(Thread.java:856)
10-26 00:56:36.052: E/AndroidRuntime(806): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
10-26 00:56:36.052: E/AndroidRuntime(806): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
10-26 00:56:36.052: E/AndroidRuntime(806): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:823)
10-26 00:56:36.052: E/AndroidRuntime(806): at android.view.View.requestLayout(View.java:15473)
10-26 00:56:36.052: E/AndroidRuntime(806): at android.view.View.setFlags(View.java:8444)
10-26 00:56:36.052: E/AndroidRuntime(806): at android.view.View.setVisibility(View.java:5716)
10-26 00:56:36.052: E/AndroidRuntime(806): at android.app.Dialog.hide(Dialog.java:294)
10-26 00:56:36.052: E/AndroidRuntime(806): at com.firstgroup.webservice.RequestTask.doInBackground(RequestTask.java:66)
10-26 00:56:36.052: E/AndroidRuntime(806): at com.firstgroup.webservice.RequestTask.doInBackground(RequestTask.java:1)
10-26 00:56:36.052: E/AndroidRuntime(806): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-26 00:56:36.052: E/AndroidRuntime(806): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-26 00:56:36.052: E/AndroidRuntime(806): ... 4 more
10-26 00:56:40.424: E/WindowManager(806): Activity com.firstgroup.applicationload.Appliacationload has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40f6b308 G.E..... R.....ID 0,0-729,324} that was originally added here
10-26 00:56:40.424: E/WindowManager(806): android.view.WindowLeaked: Activity com.firstgroup.applicationload.Appliacationload has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40f6b308 G.E..... R.....ID 0,0-729,324} that was originally added here
10-26 00:56:40.424: E/WindowManager(806): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
10-26 00:56:40.424: E/WindowManager(806): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
10-26 00:56:40.424: E/WindowManager(806): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
10-26 00:56:40.424: E/WindowManager(806): at android.app.Dialog.show(Dialog.java:281)
10-26 00:56:40.424: E/WindowManager(806): at com.firstgroup.webservice.RequestTask.onPreExecute(RequestTask.java:36)
10-26 00:56:40.424: E/WindowManager(806): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
10-26 00:56:40.424: E/WindowManager(806): at android.os.AsyncTask.execute(AsyncTask.java:534)
10-26 00:56:40.424: E/WindowManager(806): at com.firstgroup.applicationload.Appliacationload.calldeparutureWebservice(Appliacationload.java:130)
10-26 00:56:40.424: E/WindowManager(806): at com.firstgroup.applicationload.Appliacationload.access$1(Appliacationload.java:126)
10-26 00:56:40.424: E/WindowManager(806): at com.firstgroup.applicationload.Appliacationload$2.onClick(Appliacationload.java:91)
10-26 00:56:40.424: E/WindowManager(806): at android.view.View.performClick(View.java:4204)
10-26 00:56:40.424: E/WindowManager(806): at android.view.View$PerformClick.run(View.java:17355)
10-26 00:56:40.424: E/WindowManager(806): at android.os.Handler.handleCallback(Handler.java:725)
10-26 00:56:40.424: E/WindowManager(806): at android.os.Handler.dispatchMessage(Handler.java:92)
10-26 00:56:40.424: E/WindowManager(806): at android.os.Looper.loop(Looper.java:137)
10-26 00:56:40.424: E/WindowManager(806): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-26 00:56:40.424: E/WindowManager(806): at java.lang.reflect.Method.invokeNative(Native Method)
10-26 00:56:40.424: E/WindowManager(806): at java.lang.reflect.Method.invoke(Method.java:511)
10-26 00:56:40.424: E/WindowManager(806): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-26 00:56:40.424: E/WindowManager(806): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-26 00:56:40.424: E/WindowManager(806): at dalvik.system.NativeStart.main(Native Method)
Should be something like this:
#Override
public void getWebserviceResponse(String result) {
// TODO Auto-generated method stub
//JSONObject js=new Jso(result);
deparaturedaseboarddto data = new Gson().fromJson(result, deparaturedaseboarddto.class);
/*listView=(ListView)findViewById(R.id.listView1);
adapter = new CustomListAdapter(this, data.getData());
listView.setAdapter(adapter);*/
Intent i = new Intent(Appliacationload.this,Departuredashboardscreen.class);
//i.putExtra("test", data);
EventBus.getDefault().postSticky(deparaturedaseboarddto);
startActivity(i);
finish();
Log.d("----", "========");
}
and in Departuredashboardscreen
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.departure_dashboard);
deparaturedaseboarddto myParcelableObject = EventBus.getDefault().getStickyEvent(deparaturedaseboarddto.class);
Log.d("===", "hhhh");
}
Try using getExtra()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.departure_dashboard);
//use getExtra()
Intent i = getIntent().getExtras();
deparaturedaseboarddto myParcelableObject =
(deparaturedaseboarddto) i.getParcelableExtra("test");
Log.d("===", "hhhh");
}
I am writing an Android app with upload a file to Dropbox. However, I do not know
why I encounter DropboxUnlinkedException at runtime after reading some articles on StackOverflow. Can anyone tell me what problem it is?
Actually, I am new to JAVA and Android. Hope that you can explain the problem as well
as the solutions in details. Thanks a lot! :D
(Remarks: I have checked that I can call the uploadFile method. But there is no any response get. I am confused.)
public class ConnectToDropBox extends Activity {
final static private String APP_KEY = "16u74380lodkknv";
final static private String APP_SECRET = "westpz8i6mqn15g";
final static private AccessType ACCESS_TYPE = AccessType.DROPBOX;
// In the class declaration section:
private DropboxAPI<AndroidAuthSession> mDBApi;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// And later in some initialization function:
AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appKeys,
ACCESS_TYPE);
mDBApi = new DropboxAPI<AndroidAuthSession>(session);
mDBApi.getSession().startOAuth2Authentication(ConnectToDropBox.this);
try {
uploadFile();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DropboxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void onResume() {
super.onResume();
if (mDBApi.getSession().authenticationSuccessful()) {
try {
// Required to complete auth, sets the access token on the
// session
mDBApi.getSession().finishAuthentication();
String accessToken = mDBApi.getSession().getOAuth2AccessToken();
} catch (IllegalStateException e) {
Log.i("DbAuthLog", "Error authenticating", e);
}
}
}
public void uploadFile() throws DropboxException, FileNotFoundException {
File file = new File("/sdcard/letschat/data/backup.txt.part.1");
FileInputStream inputStream = new FileInputStream(file);
Entry response = mDBApi.putFile(
"/sdcard/letschat/data/backup.txt.part.1", inputStream,
file.length(), null, null);
Toast.makeText(ConnectToDropBox.this, "HIHI", Toast.LENGTH_SHORT)
.show();
}
}
The LogCat is as follows:
03-28 20:27:28.269: W/System.err(8283): com.dropbox.client2.exception.DropboxUnlinkedException
03-28 20:27:28.273: W/System.err(8283): at com.dropbox.client2.DropboxAPI.assertAuthenticated(DropboxAPI.java:2525)
03-28 20:27:28.273: W/System.err(8283): at com.dropbox.client2.DropboxAPI.putFileRequest(DropboxAPI.java:2177)
03-28 20:27:28.273: W/System.err(8283): at com.dropbox.client2.DropboxAPI.putFileRequest(DropboxAPI.java:1498)
03-28 20:27:28.273: W/System.err(8283): at com.dropbox.client2.DropboxAPI.putFile(DropboxAPI.java:1458)
03-28 20:27:28.277: W/System.err(8283): at com.tolmms.backup.ConnectToDropBox.uploadFile(ConnectToDropBox.java:74)
03-28 20:27:28.277: W/System.err(8283): at com.tolmms.backup.ConnectToDropBox.onCreate(ConnectToDropBox.java:45)
03-28 20:27:28.277: W/System.err(8283): at android.app.Activity.performCreate(Activity.java:5008)
03-28 20:27:28.277: W/System.err(8283): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-28 20:27:28.277: W/System.err(8283): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-28 20:27:28.277: W/System.err(8283): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-28 20:27:28.277: W/System.err(8283): at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-28 20:27:28.277: W/System.err(8283): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-28 20:27:28.277: W/System.err(8283): at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 20:27:28.277: W/System.err(8283): at android.os.Looper.loop(Looper.java:137)
03-28 20:27:28.277: W/System.err(8283): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-28 20:27:28.281: W/System.err(8283): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 20:27:28.281: W/System.err(8283): at java.lang.reflect.Method.invoke(Method.java:511)
03-28 20:27:28.281: W/System.err(8283): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-28 20:27:28.281: W/System.err(8283): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-28 20:27:28.281: W/System.err(8283): at dalvik.system.NativeStart.main(Native Method)
The documentation kinda sucks, it does not tell you that if you dont use onResume, how to code this. I have called the upload in a service.
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(God.TAG, "Initializing Dropbox");
AppKeyPair appKeys = new AppKeyPair(God.DROPBOX_APP_KEY,
God.DROPBOX_APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appKeys);
mDBApi = new DropboxAPI<AndroidAuthSession>(session);
mDBApi.getSession().startOAuth2Authentication(this);
if (mDBApi != null && mDBApi.getSession().authenticationSuccessful()) {
try {
// Required to complete auth, sets the access token on the session
mDBApi.getSession().finishAuthentication();
UploadDB2DropBox upload2DropBox = new UploadDB2DropBox(this,
mDBApi);
upload2DropBox.execute();
//String accessToken = mDBApi.getSession().getOAuth2AccessToken();
} catch (IllegalStateException e) {
Log.i(God.TAG, "Error authenticating", e);
}
}
return super.onStartCommand(intent, flags, startId);
}
I have been trying to program a twitter client for Android (using twitter4j). So far the idea is to have a simple GUI, and if there is not a file with the OAuth token in the SD Card, connect to the Twitter API using AsyncTask, get the URL for the authorization and open the default browser. However, the browser never runs. Depending on the different modifications I have made trying to fix this, either the Activity starts normally but the browser never starts or the Activity crashes. I have come to a point of a a little of frustation and confussion. Can someone point out what's wrong with my code?
public class StatusActivity extends Activity {
private static final String TAG = "StatusActivity";
EditText editText;
Button updateButton;
File oauthfile = null;
public Context context = getApplicationContext();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_status);
Log.d(TAG, "started");
// Find views
editText = (EditText) findViewById(R.id.editText); //
updateButton = (Button) findViewById(R.id.buttonUpdate);
oauthfile = new File("sdcard/auth_file.txt");
//Check if the file with the keys exist
if (oauthfile.exists()==false){
Log.d(TAG, "file not created");
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, "file not created.", Toast.LENGTH_SHORT);
toast.show();
new Authorization(context).execute();
}
}
public void openBrowser (View v){
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
Log.d(TAG, "onclick");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.status, menu);
return true;
}
}
class Authorization extends AsyncTask <String, Integer, String>{
String url = null;
private Context context;
Authorization(Context context) {
this.context = context.getApplicationContext();
}
public void onPreExecute() {
super.onPreExecute();
Toast.makeText(context, "Invoke onPreExecute()", Toast.LENGTH_SHORT).show();
}
#Override
public String doInBackground(String... params) {
ConfigurationBuilder configBuilder = new ConfigurationBuilder();
configBuilder.setDebugEnabled(true)
//I have eliminated the keys from the question :)
.setOAuthConsumerKey("XXXXXXXXXXXXXX")
.setOAuthConsumerSecret("XXXXXXXXXXXXXXX");
Twitter OAuthTwitter = new TwitterFactory(configBuilder.build()).getInstance();
RequestToken requestToken = null;
AccessToken accessToken = null;
do{
try {
requestToken = OAuthTwitter.getOAuthRequestToken();
url = requestToken.getAuthorizationURL();
}
catch (TwitterException ex) {
ex.printStackTrace();
}
}
while (accessToken==null);
return url;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(context, "Opening browser.", Toast.LENGTH_SHORT).show();
Intent browserIntent = new Intent(Intent.ACTION_ALL_APPS, Uri.parse(url));
context.startActivity(browserIntent);
}
}
I know that at least checks if the file for the tokens exists because the toast "file not created" appears, and that the activity is able to run the browser if I press the button. The app has permissions to write in the SD card and use the Internet. Thanks in advance.
Logcat Trace:
03-28 19:02:32.816: E/AndroidRuntime(278): FATAL EXCEPTION: main
03-28 19:02:32.816: E/AndroidRuntime(278): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.versec.pardinus/com.versec.pardinus.StatusActivity}: java.lang.NullPointerException
03-28 19:02:32.816: E/AndroidRuntime(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
03-28 19:02:32.816: E/AndroidRuntime(278): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-28 19:02:32.816: E/AndroidRuntime(278): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-28 19:02:32.816: E/AndroidRuntime(278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-28 19:02:32.816: E/AndroidRuntime(278): at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 19:02:32.816: E/AndroidRuntime(278): at android.os.Looper.loop(Looper.java:123)
03-28 19:02:32.816: E/AndroidRuntime(278): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-28 19:02:32.816: E/AndroidRuntime(278): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 19:02:32.816: E/AndroidRuntime(278): at java.lang.reflect.Method.invoke(Method.java:521)
03-28 19:02:32.816: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-28 19:02:32.816: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-28 19:02:32.816: E/AndroidRuntime(278): at dalvik.system.NativeStart.main(Native Method)
03-28 19:02:32.816: E/AndroidRuntime(278): Caused by: java.lang.NullPointerException
03-28 19:02:32.816: E/AndroidRuntime(278): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
03-28 19:02:32.816: E/AndroidRuntime(278): at com.versec.pardinus.StatusActivity.<init>(StatusActivity.java:30)
03-28 19:02:32.816: E/AndroidRuntime(278): at java.lang.Class.newInstanceImpl(Native Method)
03-28 19:02:32.816: E/AndroidRuntime(278): at java.lang.Class.newInstance(Class.java:1429)
03-28 19:02:32.816: E/AndroidRuntime(278): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-28 19:02:32.816: E/AndroidRuntime(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
03-28 19:02:32.816: E/AndroidRuntime(278): ... 11 more
This is what is causing your crash.
public Context context = getApplicationContext();
You're not even using it when you need it, so you can just get rid of this line.
Btw, something else I noticed while looking at your code is this:
oauthfile = new File("sdcard/auth_file.txt");
Don't take the "sdcard/" path for granted. Use this instead:
File dir = Environment.getExternalStorageDirectory();
File oauthfile = new File(dir, "auth_file.txt");
I have a button with set color code:
buttonClicked.setTextColor(Color.GRAY);
Now when I finish my timer, the text of all buttons reset to something else, so I need that text to go back to default. I tried this but I get error (i placed a comment near the end of my code for that error):
final OnClickListener clickListener = new OnClickListener() {
private Button buttonClicked;
private int brojacKlikova = 0;
public void onClick(View v) {
brojacKlikova++;
if (brojacKlikova < 16) {
Button button = (Button) v;
button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x003333));
if (buttonClicked == null) {
// first button is clicked
buttonClicked = button;
} else {
// second button is clicked
if (buttonClicked.getTag().equals(button.getTag())) {
button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
buttonClicked.setEnabled(false);
button.setEnabled(false);
counter = counter + 5;
score.setText("Poeni: " + counter);
} else {
buttonClicked.setEnabled(false);
buttonClicked.setTextColor(Color.GRAY);
buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFCC99));
button.getBackground().clearColorFilter();
}
buttonClicked = null;
}
}else{
buttonClicked = null;
buttonClicked.setTextColor(Color.WHITE); //<--- here I get error
brojacKlikova = 0;
brojacVremena.cancel();
mHandler.postDelayed(mLaunchTask,2200);
}
}
};
How to reset my button's text color to default?
Here's logcat:
03-28 23:59:29.357: W/dalvikvm(27918): threadid=1: thread exiting with uncaught exception (group=0x40018560)
03-28 23:59:29.467: E/AndroidRuntime(27918): FATAL EXCEPTION: main
03-28 23:59:29.467: E/AndroidRuntime(27918): java.lang.NullPointerException
03-28 23:59:29.467: E/AndroidRuntime(27918): at rs.androidaplikacije.spojnice.Spojnice$1.onClick(Spojnice.java:77)
03-28 23:59:29.467: E/AndroidRuntime(27918): at android.view.View.performClick(View.java:2506)
03-28 23:59:29.467: E/AndroidRuntime(27918): at android.view.View$PerformClick.run(View.java:9112)
03-28 23:59:29.467: E/AndroidRuntime(27918): at android.os.Handler.handleCallback(Handler.java:587)
03-28 23:59:29.467: E/AndroidRuntime(27918): at android.os.Handler.dispatchMessage(Handler.java:92)
03-28 23:59:29.467: E/AndroidRuntime(27918): at android.os.Looper.loop(Looper.java:130)
03-28 23:59:29.467: E/AndroidRuntime(27918): at android.app.ActivityThread.main(ActivityThread.java:3835)
03-28 23:59:29.467: E/AndroidRuntime(27918): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 23:59:29.467: E/AndroidRuntime(27918): at java.lang.reflect.Method.invoke(Method.java:507)
03-28 23:59:29.467: E/AndroidRuntime(27918): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
03-28 23:59:29.467: E/AndroidRuntime(27918): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
03-28 23:59:29.467: E/AndroidRuntime(27918): at dalvik.system.NativeStart.main(Native Method)
03-28 23:59:31.167: I/Process(27918): Sending signal. PID: 27918 SIG: 9
buttonClicked = null;
buttonClicked.setTextColor(Color.WHITE);
Problem is here. You are assigned button to NULL and then you call property on it. This always throw NPE. Solution is to remove first line and it should work.