This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I learn android studio programing and I have problem in my programs,
when I want to go to new activity using intent statement my app crashed,
this is my main activity;
public class MainActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
}
public void onStart(){
super.onStart();
FirebaseUser CurrentUser = mAuth.getCurrentUser();
if(CurrentUser == null){
Intent StartIntent = new Intent(MainActivity.this,
StartActivity.class);
startActivity(StartIntent);
finish();
}
}
}
and this is manifest :
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".StartActivity"
android:label="#string/app_name">
</activity>
there is no error message
but thit is what i have into android monitor :
08-23 21:50:33.507 11264-11264/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ruse.ayse.areyousmartenough, PID: 11264
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ruse.ayse.areyousmartenough/com.ruse.ayse.areyousmartenough.StartActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2378)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5433)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.ruse.ayse.areyousmartenough.StartActivity.onCreate(StartActivity.java:31)
at android.app.Activity.performCreate(Activity.java:5301)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2291)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2378)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5433)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
I solved this problem into another project by adding "intent-filter" into manifest file but it does not work with this project,
this is StartActivity :
public class StartActivity extends AppCompatActivity {
private Button mRegBtn ;
private Button mLoginBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
mRegBtn = (Button) findViewById(R.id.RegBtn);
mLoginBtn = (Button) findViewById(R.id.LoginBtn);
mRegBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent createAccount = new Intent(StartActivity.this, RegisterActivity.class);
startActivity(createAccount);
}
});
mLoginBtn.setOnClickListener(new View.OnClickListener(){ /////this is line 31
#Override
public void onClick(View view) {
Intent loginAccount = new Intent(StartActivity.this, LoginActivity.class);
startActivity(loginAccount);
}
});
}}
Your problem is at StartActivity.java:31 you are trying to read from a null reference.
By your comment, you look to miss getting the mLoginBtn from your Layout=
mLoginBtn = (Button) findViewById(R.id.btn_login);
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
I'm very new to android, just took it as a weekend development for fun, trying to make a very simple app and I'm having an error.
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gwallet">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
Context context;
final TextView txtNewDailyDetailDescription = findViewById(R.id.txt_new_daily_detail_description);
final TextView txtNewDailyDetailSpend = findViewById(R.id.txt_new_daily_detail_spend);
final Button btnNewDailyDetail = findViewById(R.id.btn_new_daily_detail);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
txtNewDailyDetailSpend.setInputType(InputType.TYPE_CLASS_NUMBER);
btnNewDailyDetail.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
btnNewDailyDetailClick();
}
});
}
protected void btnNewDailyDetailClick() {
//Some irrelevant code.
}
}
Stack trace of the error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.gwallet, PID: 32507
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.gwallet/com.gwallet.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3027)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3264)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7091)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:164)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
at android.content.Context.obtainStyledAttributes(Context.java:677)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:692)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:479)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:214)
at com.gwallet.MainActivity.<init>(MainActivity.java:18)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
at android.app.Instrumentation.newActivity(Instrumentation.java:1219)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3015)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3264)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7091)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
I do get that something that is not initialized is being tried to get in use, but I don't know what it is. I keep reading Activities documentation. Any help?
Let me know if this work!
public class MainActivity extends AppCompatActivity {
Context context;
TextView txtNewDailyDetailDescription;
TextView txtNewDailyDetailSpen;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtNewDailyDetailDescription=findViewById(R.id.txt_new_daily_detail_description);
txtNewDailyDetailSpend = findViewById(R.id.txt_new_daily_detail_spend);
btnNewDailyDetail = findViewById(R.id.btn_new_daily_detail);
context = this;
txtNewDailyDetailSpend.setInputType(InputType.TYPE_CLASS_NUMBER);
btnNewDailyDetail.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
btnNewDailyDetailClick();
}
});
}
protected void btnNewDailyDetailClick() {
//Some irrelevant code.
}
}
Well, I am totally a beginner, I was trying to do some exercise for webview. However, I can't find what problem it is. Can someone helps me? Thanks.
Here is my java class:
public class MainActivity extends AppCompatActivity {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1)
;
Uri uri = Uri.parse("www.google.com");
final Intent intent = new Intent(Intent.ACTION_VIEW,uri);
button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(intent);
}
}
);
}
}
And here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jinyu.webview">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here is my log:
4-16 22:52:03.234 23525-23525/com.example.jinyu.webview E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jinyu.webview, PID: 23525
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=www.baidu.com }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1798)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
at android.app.Activity.startActivityForResult(Activity.java:3930)
at android.app.Activity.startActivityForResult(Activity.java:3890)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
at android.app.Activity.startActivity(Activity.java:4213)
at android.app.Activity.startActivity(Activity.java:4181)
at com.example.jinyu.webview.MainActivity$1.onClick(MainActivity.java:29)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-16 22:52:04.687 23525-23525/com.example.jinyu.webview I/Process: Sending signal. PID: 23525 SIG: 9
add "http://" or "https://" to your url.
Edit (for future visitors) : add http:// or https:// before actual url.
Shortest way to achieve this is
public class MainActivity extends AppCompatActivity {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
}
}
);
}
}
I created a scrolling activity but when I debug the app and click on the button to open the activity, I get an error. Basically, it crashes the whole app. However, here's my code below.
Is it because it's a scrolling activity and not an empty activity?
Here is the java file:
news.java
package com.example.it5.foothillers;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class news extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
main_acitivity.java
package com.example.it5.foothillers;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
private void buttonClick() {
startActivity(new Intent("it5.foothillers.news"));
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
buttonClick();
break;
}
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.it5.foothillers">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".news">
<intent-filter>
<action android:name="it5.foothillers.news" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Logs:
03-30 12:01:49.871 2122-2122/com.example.it5.foothillers E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.it5.foothillers, PID: 2122
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.it5.foothillers/com.example.it5.foothillers.news}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:197)
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:129)
at com.example.it5.foothillers.news.onCreate(news.java:17)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
This Activity already has an action bar supplied by the window decor.
Your exception message is clearly telling you that you already have a Toolbar as part of the Activity's theme.
You should set a theme like this in the styles.xml file if you want to call setSupportActionBar.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
You could probably also just remove these two lines
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
And replace with
ActionBar toolbar = getSupportActionBar();
Intent intent=new Intent(MainActivity.this,news.class);
startActivity(intent);
Mainifest.xml
<activity android:name="com.example.it5.foothillers.news" ></activity>
The easiest way to start an activity, is change the intent to this:
startActivity(new Intent(MainActivity.this, news.class));
The stack trace shows you're attempting to add another ActionBar and you already had one provided by the application's theme.
So, you can change the theme at yout activity declaration on manifest to not use an ActionBar, like that:
<activity android:name=".news"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="it5.foothillers.news" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Or you just remove the toolbar from your activity code and your activity_news.xml (because you're not using it later):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
Splash.java
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final ImageView iv = (ImageView) findViewById(R.id.imageView);
final Animation an = AnimationUtils.loadAnimation(getBaseContext(), R.anim.rotate);
final Animation an2 = AnimationUtils.loadAnimation(getBaseContext(), R.anim.abc_fade_out);
iv.startAnimation(an);
an.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
iv.startAnimation(an2);
finish();
Intent i = new Intent(Splash.this, MainActivity.class);
startActivity(i);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button gsbtn = (Button) findViewById(R.id.gsbtn);
Button gstbtn = (Button) findViewById(R.id.gstbtn);
Button nhbtn = (Button) findViewById(R.id.nhbtn);
Button settingsbtn = (Button) findViewById(R.id.settingsbtn);
Button exitbtn = (Button) findViewById(R.id.exitbtn);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
this is my code for main activity and splash but as soon as the emulator hits the main acticivty, the apps breaks and displays the error
"Unfortunately the app has stopped working"
The event log is not showing any bad errors but I do get this in monitor section;
02-25 22:23:17.425 2197-2197/uk.ac.hud.compugeek E/AndroidRuntime:
FATAL EXCEPTION: main
Process: uk.ac.hud.compugeek, PID: 2197
android.content.ActivityNotFoundException: Unable to find explicit activity class {uk.ac.hud.compugeek/uk.ac.hud.compugeek.MainActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1794)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
at android.app.Activity.startActivityForResult(Activity.java:3917)
at android.app.Activity.startActivityForResult(Activity.java:3877)
at android.app.Activity.startActivity(Activity.java:4200)
at android.app.Activity.startActivity(Activity.java:4168)
at uk.ac.hud.compugeek.Splash$1.onAnimationEnd(Splash.java:41)
at android.view.animation.AnimationSet.getTransformation(AnimationSet.java:400)
at android.view.animation.Animation.getTransformation(Animation.java:943)
at android.view.View.applyLegacyAnimation(View.java:15771)
at android.view.View.draw(View.java:15887)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.support.design.widget.CoordinatorLayout.drawChild(CoordinatorLayout.java:1077)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.draw(View.java:16181)
at android.view.View.updateDisplayListIfDirty(View.java:15174)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:281)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:287)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:322)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2615)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2434)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2067)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-25 22:23:21.874 2197-2197/uk.ac.hud.compugeek I/Process: Sending signal. PID: 2197 SIG: 9
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="uk.ac.hud.compugeek">
<application
android:allowBackup="true"
android:icon="#mipmap/logo"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity" ></activity>
<activity
android:name=".Splash"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
All activities need to be added to the manifest.
<activity
android:name=".MainActivity">
</activity>
I will start from my approach I want to have one service which is running even if none of application screen is visible for user. This service will scan for beacons.
Every screen of application needs to get access to method of service so I used binding to service here.
I designed serviceconnector class which will be connecting Activities with service, this class look like this.
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
public class ServiceConnector {
Context context;
BeaconScanningService scanningService;
boolean mBound = false;
public ServiceConnector(Context context) {
LogShower.printLogs("Service Connector created");
this.context = context;
createBinding();
}
public void createBinding()
{
Intent intent = new Intent(context, BeaconScanningService.class);
if(scanningService.isBeaconScanningServiceRunning())
{
LogShower.printLogs("Service is already running.");
context.bindService(intent, mConnection, 0);
}
else
{
LogShower.printLogs("Service is not running yet.");
context.startService(new Intent(context, BeaconScanningService.class));
context.bindService(intent, mConnection, 0);
}
}
public void startScanning()
{
LogShower.printLogs("Start Scanning.");
scanningService.startBeaconScanner();
}
public void stopScanning()
{
LogShower.printLogs("Stop Scanning.");
scanningService.stopBeaconScanner();
}
public void destroyBinding()
{
if (mBound) {
scanningService.unbindService(mConnection);
mBound = false;
}
}
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className,
IBinder service)
{
LogShower.printLogs("onServiceConnected");
// We've bound to LocalService, cast the IBinder and get LocalService instance
BeaconScanningService.LocalBinder binder = (BeaconScanningService.LocalBinder) service;
scanningService = binder.getService();
mBound = true;
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
LogShower.printLogs("onServiceDisconnected");
mBound = false;
}
};
}
This is activity when I make binding and try to unbind
import android.app.Activity;
import android.os.Bundle;
public class StartScreen extends Activity
{
ServiceConnector serviceConnector ;
#Override
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.starting_screen_layout);
serviceConnector = new ServiceConnector(this);
}
#Override
protected void onDestroy ()
{
serviceConnector.destroyBinding();
super.onDestroy();
}
}
Name of service is BeaconScanningService and this is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bka.tog.ole" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".ZooBeacon"
android:label="#string/app_name" >
</activity>
<service android:enabled="true" android:name=".beaconeserviceandconnector.BeaconScanningService">
</service>
<activity
android:name=".StartScreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The problem is that I don't know why my ServiceConnector class is interprete as Service during unbinding process or just I can't make analysis of this logcat.
3405-3405/com.bka.tog.ole E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.bka.tog.ole, PID: 3405
java.lang.RuntimeException: Unable to destroy activity {com.bka.tog.ole/com.bka.tog.ole.StartScreen}: java.lang.IllegalArgumentException: Service not registered: com.bka.tog.ole.beaconeserviceandconnector.ServiceConnector$1#41d2f8d0
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3647)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3665)
at android.app.ActivityThread.access$1400(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1299)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5212)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Service not registered: com.bka.tog.ole.beaconeserviceandconnector.ServiceConnector$1#41d2f8d0
at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:973)
at android.app.ContextImpl.unbindService(ContextImpl.java:1671)
at android.content.ContextWrapper.unbindService(ContextWrapper.java:536)
at com.bka.tog.ole.beaconeserviceandconnector.ServiceConnector.destroyBinding(ServiceConnector.java:59)
at com.bka.tog.ole.StartScreen.onDestroy(StartScreen.java:27)
at android.app.Activity.performDestroy(Activity.java:5412)
at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1118)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3634)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3665)
at android.app.ActivityThread.access$1400(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1299)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5212)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
What is the reason of this behavior?
In my ServiceConnector class should be:
context.unbindService(mConnection);
instead of this line:
scanningService.unbindService(mConnection);
I don't know how this misscoding come from, but I lost few hours, so be sure that you unbind from context of activity.
Normally i would delete this question, but one person make it favorite so I want to show what was the reason of my error.