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)
Related
I used Android native Sip api to initialize Sip client.Because I am confused to Sip protocol,I read the android developer document about sip and follow it.When I run my application on Nexus5,it work normally,but when I run it on other device like coolpad, it throw NullPointException from SipManager.The following is my code.For some reason,the username,passwd and domain is private.
public static SipManager manager;
public static SipProfile me;
public static String username;
public static String passwd;
public static void initializeSip(Context context) {
System.out.println("initializeSip");
if (manager == null) {
manager = SipManager.newInstance(context);
}
if (me != null) {
closeLocalProfile();
}
username = SharedPreferences.getLoginInfo().getModel().getVos_phone();
passwd = SharedPreferences.getLoginInfo().getModel().getVos_phone_pwd();
try {
if (!(SipManager.isApiSupported(context) && SipManager.isVoipSupported(context))) {
System.out.println("cannot support");
return;
}
System.out.println("isApiSupported="+SipManager.isApiSupported(context));
System.out.println("SipManager="+SipManager.newInstance(context));
SipProfile.Builder builder = new SipProfile.Builder(username, domain);
builder.setPassword(passwd);
me = builder.build();
manager.open(me);
SipRegistrationListener listener = new SipRegistrationListener() {
#Override
public void onRegistering(String localProfileUri) {
System.out.println("Registering with SIP Server");
}
#Override
public void onRegistrationDone(String localProfileUri, long expiryTime) {
System.out.println("Register success");
}
#Override
public void onRegistrationFailed(String localProfileUri, int errorCode, String errorMessage) {
System.out.println("Registeration failed.Please check Settings.");
System.out.println("errorCode="+errorCode);
System.out.println("errorMessage="+errorMessage);
}
};
manager.setRegistrationListener(me.getUriString(),listener);
manager.register(me,3000,listener);
} catch (ParseException e) {
e.printStackTrace();
} catch (SipException e) {
e.printStackTrace();
}
}
The following is my logcat. Most strangely,I find that the api isApiSupported return true and the SipManager object is not null.I cannot find the reason about it.
1.741 30654-30654/com.dev.goldunion I/System.out: initializeSip
05-18 20:11:41.741 30654-30654/com.dev.goldunion I/System.out: isApiSupported=true
05-18 20:11:41.751 30654-30654/com.dev.goldunion I/System.out: SipManager=android.net.sip.SipManager#426af938
05-18 20:11:41.751 30654-30654/com.dev.goldunion D/AndroidRuntime: Shutting down VM
05-18 20:11:41.751 30654-30654/com.dev.goldunion W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41692970)
05-18 20:11:41.751 30654-30654/com.dev.goldunion E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dev.goldunion/com.dev.goldunion.activity.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2227)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2282)
at android.app.ActivityThread.access$600(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5265)
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:760)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.net.sip.SipManager.open(SipManager.java:180)
at com.dev.goldunion.util.RegisterSipAccountUtils.initializeSip(RegisterSipAccountUtils.java:49)
at com.dev.goldunion.activity.MainActivity.onCreate(MainActivity.java:77)
at android.app.Activity.performCreate(Activity.java:5146)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2191)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2282)
at android.app.ActivityThread.access$600(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5265)
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:760)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
at dalvik.system.NativeStart.main(Native Method)
I have an application which is using google maps and listening on camera change. My problem is that on each camera change I have to request my backend. What I want to do is just limiting the number of requests by using RxAndroid/Java to debounce.
My code looks like this:
Observable.create(new Observable.OnSubscribe<CameraPosition>() {
#Override
public void call(Subscriber<? super CameraPosition> subscriber) {
if (!subscriber.isUnsubscribed()) {
map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition cameraPosition) {
subscriber.onNext(cameraPosition);
}
});
}
}
}).subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
.onErrorResumeNext(Observable.<CameraPosition>empty())
.debounce(1, TimeUnit.SECONDS)
.subscribe(cameraPosition -> {
final LatLngBounds item = map.getProjection().getVisibleRegion().latLngBounds;
homeActionBarActivity.getNMB().getRide().list(
item.southwest.latitude,
item.southwest.longitude,
item.northeast.latitude,
item.northeast.longitude)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.onErrorResumeNext(Observable.<List<Ride>>empty())
.subscribe(new Action1<List<Ride>>() {
#Override
public void call(List<Ride> rides) {
clearRidePointsFromMap();
for (Ride ride : rides) {
if (isAdded())
addRideStartPointToMap(ride);
}
}
});
});
As you can see, I am forcing to use the MainThread for both (subscribeOn and observeOn) but I still get this error "Not on the main thread".
Here the stacktrace:
01-27 10:59:24.049 3049-3066/com.nousmotards.android E/AndroidRuntime﹕ FATAL EXCEPTION: RxComputationThreadPool-2
Process: com.nousmotards.android, PID: 3049
java.lang.IllegalStateException: Exception thrown on Scheduler.Worker thread. Add `onError` handling.
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:52)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:864)
Caused by: rx.exceptions.OnErrorNotImplementedException: Not on the main thread
at rx.Observable$31.onError(Observable.java:7134)
at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:154)
at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:111)
at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:137)
at rx.observers.SerializedObserver.onNext(SerializedObserver.java:159)
at rx.observers.SerializedSubscriber.onNext(SerializedSubscriber.java:81)
at rx.internal.operators.OperatorDebounceWithTime$DebounceState.emit(OperatorDebounceWithTime.java:128)
at rx.internal.operators.OperatorDebounceWithTime$1$1.call(OperatorDebounceWithTime.java:72)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:47)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:864)
Caused by: java.lang.IllegalStateException: Not on the main thread
at com.google.k.a.cl.b(Unknown Source)
at com.google.maps.api.android.lib6.c.ca.a(Unknown Source)
at com.google.maps.api.android.lib6.c.el.l(Unknown Source)
at com.google.android.gms.maps.internal.l.onTransact(SourceFile:312)
at android.os.Binder.transact(Binder.java:361)
at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.getProjection(Unknown Source)
at com.google.android.gms.maps.GoogleMap.getProjection(Unknown Source)
at com.nousmotards.android.fragments.home.MapFragment.lambda$onViewCreated$33(MapFragment.java:117)
at com.nousmotards.android.fragments.home.MapFragment.access$lambda$0(MapFragment.java)
at com.nousmotards.android.fragments.home.MapFragment$$Lambda$1.call(Unknown Source)
at rx.Observable$31.onNext(Observable.java:7139)
at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:130)
at rx.observers.SerializedObserver.onNext(SerializedObserver.java:159)
at rx.observers.SerializedSubscriber.onNext(SerializedSubscriber.java:81)
at rx.internal.operators.OperatorDebounceWithTime$DebounceState.emit(OperatorDebounceWithTime.java:128)
at rx.internal.operators.OperatorDebounceWithTime$1$1.call(OperatorDebounceWithTime.java:72)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:47)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:864)
Do you have any idea ?
Note: if I get out map.setOnCameraChangeListerner(...) from Observable.create(...) it works correctly.
Ok it seems that is (probably) due to the RxJava internal on how it is managed to pass objects from subscriber to observers. I fix my problem by simply getting LatLngBounds inside the subscriber and not the observer.
Observable.create(new Observable.OnSubscribe<LatLngBounds>() {
#Override
public void call(Subscriber<? super LatLngBounds> subscriber) {
if (!subscriber.isUnsubscribed()) {
map.setOnCameraChangeListener(cameraPosition ->
subscriber.onNext(map.getProjection().getVisibleRegion().latLngBounds));
}
}
}).subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
.onErrorResumeNext(Observable.<LatLngBounds>empty())
.debounce(1, TimeUnit.SECONDS)
.subscribe(item -> {
homeActionBarActivity.getNMB().getRide().list(
item.southwest.latitude,
item.southwest.longitude,
item.northeast.latitude,
item.northeast.longitude)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.onErrorResumeNext(Observable.<List<Ride>>empty())
.subscribe(new Action1<List<Ride>>() {
#Override
public void call(List<Ride> rides) {
clearRidePointsFromMap();
for (Ride ride : rides) {
if (isAdded())
addRideStartPointToMap(ride);
}
}
});
});
Hope this will help :)
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 trying to use a AlertDialog in my application, but it requires an input to be given. So i was forced to create an interface and override the onAttach() method within my custom DialogFragment class. Once i properly set everything in both the Dialog class and MainActivity, i tried to run my app and it gave me a runtime exception referring to nullpointer among other things. im not sure why this is and i am in need of assistance, please help. Below you will wind the Override of my interface method, the place where my .show() method is called, the code for my DialogFragment class, and my error log lastly.
public class MainActivity extends FragmentActivity implements CDia_exp.NDListener
{
//Integers and Strings for performing calculations
int a;
int b;
static int deci_cnt;
int cnt;
int temp;
Double exp_x;
Double exp;
Double [] num_trk;
String [] op_trk;
String num_hold;
String op_hold;
String del_hold;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//....Lots of other code here.......
//Exp_x Button
opSet[6].setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
if(num_hold != "")
{
cDia_exp.show(getSupportFragmentManager(), "exp_x");
del_hold = tViews[0].getText().toString();
for(int x = num_hold.length() - 1; x >= 0; x--)
{
del_hold = del_hold.substring(0, del_hold.length() - 1);
}
num_hold = exp.toString();
tViews[0].setText(del_hold + num_hold);
return;
}
else
{
return;
}
}
});
}
//Objects and Overrides for calling foreign functions
CTrim cTrim = new CTrim();
CDia cDia = new CDia();
CDia_exp cDia_exp = new CDia_exp();
EditText exp_inp = (EditText) findViewById(R.id.exp_inp);
#Override
public void onDPClick(DialogFragment dialog)
{
exp = Double.parseDouble(num_hold);
exp_x = Double.parseDouble(exp_inp.getText().toString());
for(double x = exp_x; x > 0; x--)
{
exp *= exp;
}
}
#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;
}
}
My Custom DialogFragment class
package com.example.musicalc;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
public class CDia_exp extends DialogFragment
{
public interface NDListener
{
public void onDPClick(DialogFragment dialog);
}
NDListener expListener;
#Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
try
{
expListener = (NDListener) activity;
}
catch(ClassCastException e)
{
throw new ClassCastException(activity.toString() + "does not implement NDListener");
}
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder aDia = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
aDia.setView(inflater.inflate(R.layout.calc_dia, null)).setPositiveButton(R.string.diaOk_b, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int id)
{
expListener.onDPClick(CDia_exp.this);
}
}).setNegativeButton(R.string.cancel_b, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int id)
{
getDialog().cancel();
}
});
return aDia.create();
}
}
And Now the Error Log
07-20 14:23:43.599: E/Trace(24173): error opening trace file: No such file or directory (2)
07-20 14:23:43.622: D/AndroidRuntime(24173): Shutting down VM
07-20 14:23:43.622: W/dalvikvm(24173): threadid=1: thread exiting with uncaught exception (group=0x4137d2a0)
07-20 14:23:43.653: E/AndroidRuntime(24173): FATAL EXCEPTION: main
07-20 14:23:43.653: E/AndroidRuntime(24173): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.musicalc/com.example.musicalc.MainActivity}: java.lang.NullPointerException
07-20 14:23:43.653: E/AndroidRuntime(24173): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2060)
07-20 14:23:43.653: E/AndroidRuntime(24173): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)
07-20 14:23:43.653: E/AndroidRuntime(24173): at android.app.ActivityThread.access$700(ActivityThread.java:141)
07-20 14:23:43.653: E/AndroidRuntime(24173): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)
07-20 14:23:43.653: E/AndroidRuntime(24173): at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 14:23:43.653: E/AndroidRuntime(24173): at android.os.Looper.loop(Looper.java:137)
07-20 14:23:43.653: E/AndroidRuntime(24173): at android.app.ActivityThread.main(ActivityThread.java:5059)
07-20 14:23:43.653: E/AndroidRuntime(24173): at java.lang.reflect.Method.invokeNative(Native Method)
07-20 14:23:43.653: E/AndroidRuntime(24173): at java.lang.reflect.Method.invoke(Method.java:511)
07-20 14:23:43.653: E/AndroidRuntime(24173): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
07-20 14:23:43.653: E/AndroidRuntime(24173): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
07-20 14:23:43.653: E/AndroidRuntime(24173): at dalvik.system.NativeStart.main(Native Method)
07-20 14:23:43.653: E/AndroidRuntime(24173): Caused by: java.lang.NullPointerException
07-20 14:23:43.653: E/AndroidRuntime(24173): at android.app.Activity.findViewById(Activity.java:1851)
07-20 14:23:43.653: E/AndroidRuntime(24173): at com.example.musicalc.MainActivity.<init>(MainActivity.java:43)
07-20 14:23:43.653: E/AndroidRuntime(24173): at java.lang.Class.newInstanceImpl(Native Method)
07-20 14:23:43.653: E/AndroidRuntime(24173): at java.lang.Class.newInstance(Class.java:1319)
07-20 14:23:43.653: E/AndroidRuntime(24173): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
07-20 14:23:43.653: E/AndroidRuntime(24173): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2051)
07-20 14:23:43.653: E/AndroidRuntime(24173): ... 11 more
07-20 14:25:56.817: E/Trace(24308): error opening trace file: No such file or directory (2)
07-20 14:25:56.934: D/AndroidRuntime(24308): Shutting down VM
07-20 14:25:56.934: W/dalvikvm(24308): threadid=1: thread exiting with uncaught exception (group=0x4137d2a0)
07-20 14:25:57.013: E/AndroidRuntime(24308): FATAL EXCEPTION: main
07-20 14:25:57.013: E/AndroidRuntime(24308): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.musicalc/com.example.musicalc.MainActivity}: java.lang.NullPointerException
07-20 14:25:57.013: E/AndroidRuntime(24308): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2060)
07-20 14:25:57.013: E/AndroidRuntime(24308): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)
07-20 14:25:57.013: E/AndroidRuntime(24308): at android.app.ActivityThread.access$700(ActivityThread.java:141)
07-20 14:25:57.013: E/AndroidRuntime(24308): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)
07-20 14:25:57.013: E/AndroidRuntime(24308): at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 14:25:57.013: E/AndroidRuntime(24308): at android.os.Looper.loop(Looper.java:137)
07-20 14:25:57.013: E/AndroidRuntime(24308): at android.app.ActivityThread.main(ActivityThread.java:5059)
07-20 14:25:57.013: E/AndroidRuntime(24308): at java.lang.reflect.Method.invokeNative(Native Method)
07-20 14:25:57.013: E/AndroidRuntime(24308): at java.lang.reflect.Method.invoke(Method.java:511)
07-20 14:25:57.013: E/AndroidRuntime(24308): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
07-20 14:25:57.013: E/AndroidRuntime(24308): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
07-20 14:25:57.013: E/AndroidRuntime(24308): at dalvik.system.NativeStart.main(Native Method)
07-20 14:25:57.013: E/AndroidRuntime(24308): Caused by: java.lang.NullPointerException
07-20 14:25:57.013: E/AndroidRuntime(24308): at android.app.Activity.findViewById(Activity.java:1851)
07-20 14:25:57.013: E/AndroidRuntime(24308): at com.example.musicalc.MainActivity.<init>(MainActivity.java:803)
07-20 14:25:57.013: E/AndroidRuntime(24308): at java.lang.Class.newInstanceImpl(Native Method)
07-20 14:25:57.013: E/AndroidRuntime(24308): at java.lang.Class.newInstance(Class.java:1319)
07-20 14:25:57.013: E/AndroidRuntime(24308): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
07-20 14:25:57.013: E/AndroidRuntime(24308): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2051)
07-20 14:25:57.013: E/AndroidRuntime(24308): ... 11 more
Thanks to some experimentation and some research on stackoverflow i have located the answer.
The problem is that the compiler was having trouble locating the view either because it is in a separate xml file from my activity_main.xml file or because i was creating my dialog within a separate class from my MainActiviy.java.
To solve this i created my dialog within my MainActivity.java class outside of the onCreate method. Also i created a layout inflater to inflate my layout within a View in my MainActivity, which was key because the LayoutInflater provided a way to directly call my EditText into code.
The code that fixed the nullpointer when declaring my EditText is as follows:
LayoutInflater inflater = LayoutInflater.from(this);
//Inflater "inflates" layout into View as a value
final View infV = inflater.inflate(R.layout.calc_dia, null);
//Use layout value inflated in View "infV" to find EditText
final EditText eDTemp = (EditText) infV.findViewById(R.id.expInp);
***The full code for the dialog is as follows****:
public void showCD()
{
LayoutInflater inflater = LayoutInflater.from(this);
final View infV = inflater.inflate(R.layout.calc_dia, null);
final EditText eDTemp = (EditText)infV.findViewById(R.id.expInp);
TextView tView0 = (TextView)findViewById(R.id.calcView);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(infV).setPositiveButton("Ok", new
DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
}}).setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
AlertDialog alert = builder.create();
builder.show();
}
When you are ready for the dialog to appear, simply call the function that contains it.
I am writing an app and When I try to load the class/layout by pressing a button on my main menu, I get the following logcat errors and I do not know what they mean, can anyone tell me why my app is crashing?
03-11 16:40:06.955: E/AndroidRuntime(18456): FATAL EXCEPTION: main
03-11 16:40:06.955: E/AndroidRuntime(18456): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.famouspeople/com.example.famouspeople.Music}: java.lang.InstantiationException: can't instantiate class com.example.famouspeople.Music; no empty constructor
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.os.Looper.loop(Looper.java:137)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.app.ActivityThread.main(ActivityThread.java:4898)
03-11 16:40:06.955: E/AndroidRuntime(18456): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 16:40:06.955: E/AndroidRuntime(18456): at java.lang.reflect.Method.invoke(Method.java:511)
03-11 16:40:06.955: E/AndroidRuntime(18456): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
03-11 16:40:06.955: E/AndroidRuntime(18456): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
03-11 16:40:06.955: E/AndroidRuntime(18456): at dalvik.system.NativeStart.main(Native Method)
03-11 16:40:06.955: E/AndroidRuntime(18456): Caused by: java.lang.InstantiationException: can't instantiate class com.example.famouspeople.Music; no empty constructor
03-11 16:40:06.955: E/AndroidRuntime(18456): at java.lang.Class.newInstanceImpl(Native Method)
03-11 16:40:06.955: E/AndroidRuntime(18456): at java.lang.Class.newInstance(Class.java:1319)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.app.Instrumentation.newActivity(Instrumentation.java:1057)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
03-11 16:40:06.955: E/AndroidRuntime(18456): ... 11 more
03-11 16:40:16.560: I/Process(18456): Sending signal. PID: 18456 SIG: 9
Code:
public class Music implements OnCompletionListener{
MediaPlayer mediaPlayer;
boolean isPrepared = false;
public Music(AssetFileDescriptor assetDescriptor){
mediaPlayer = new MediaPlayer();
try{
mediaPlayer.setDataSource(assetDescriptor.getFileDescriptor(), assetDescriptor.getStartOffset(), assetDescriptor.getLength());
mediaPlayer.prepare();
isPrepared = true;
mediaPlayer.setOnCompletionListener(this);
} catch(Exception ex){
throw new RuntimeException("Couldn't load music, uh oh!");
}
}
public Music(FileDescriptor fileDescriptor){
mediaPlayer = new MediaPlayer();
try{
mediaPlayer.setDataSource(fileDescriptor);
mediaPlayer.prepare();
isPrepared = true;
mediaPlayer.setOnCompletionListener(this);
} catch(Exception ex){
throw new RuntimeException("Couldn't load music, uh oh!");
}
}
public void onCompletion(MediaPlayer mediaPlayer) {
synchronized(this){
isPrepared = false;
}
}
public void play() {
if(mediaPlayer.isPlaying()){
return;
}
try{
synchronized(this){
if(!isPrepared){
mediaPlayer.prepare();
}
mediaPlayer.start();
}
} catch(IllegalStateException ex){
ex.printStackTrace();
} catch(IOException ex){
ex.printStackTrace();
}
}
public void stop() {
mediaPlayer.stop();
synchronized(this){
isPrepared = false;
}
}
public void switchTracks(){
mediaPlayer.seekTo(0);
mediaPlayer.pause();
}
public void pause() {
mediaPlayer.pause();
}
public boolean isPlaying() {
return mediaPlayer.isPlaying();
}
public boolean isLooping() {
return mediaPlayer.isLooping();
}
public void setLooping(boolean isLooping) {
mediaPlayer.setLooping(isLooping);
}
public void setVolume(float volumeLeft, float volumeRight) {
mediaPlayer.setVolume(volumeLeft, volumeRight);
}
public void dispose() {
if(mediaPlayer.isPlaying()){
stop();
}
mediaPlayer.release();
}
}
You need to define the no argument constructor otherwise the systems doesn't know how to instantiate it. Dalvik VM looking for a zero-argument constructor. If you define one argument or more than one in a service.
public Music() {
....
}
Android instrumentation is trying to create an instance of com.example.famouspeople.Music and for that it needs a default constructor. That is why it has failed. Creating a default constructor, public com.example.famouspeople.Music(){} in the Music class could help.
public class Music implements OnCompletionListener{
public Music ()
{
}
public boolean setFileDescriptor(FileDescriptor fileDescriptor){
//set file descriptor for the current instance
}
public boolean init(){
//do initialisation of player
}
}
call init() after setting file descriptor. But the whole naming conventions seems confusing for your package/classes.