This question already has answers here:
How to start new activity on button click
(28 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I am currently breaking my head off, because I can start a normal startActivity(). It is always giving me this error:
Attempt to invoke virtual method
'android.app.ActivityThread$ApplicationThread
android.app.ActivityThread.getApplicationThread()' on a null object
reference
I am starting it from my Application
Welcome Activity:
application.getUserFromDatabase(getApplicationContext(), username, password);
Application:
public void getUserFromDatabase(Context context, String username, String password) {
//new GetFromDatabase().execute("getUser.php", "?username=" + username + "&password=" + password, this, context);
WelcomeActivity activity = new WelcomeActivity();
activity.startMainActivity();
}
AndroidManifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.mydomain.app">
<application
android:name=".MainApplication"
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">
</activity>
<activity android:name=".WelcomeActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Thank you in advance :)
EDIT:
I forgot adding my Method startMainActivity here:
public void startMainActivity(){
Intent startMain = new Intent(getApplicationContext(), MainActivity.class)
startActivity(startMain);
}
you must use intent for startActivity();
example:
Intent intent = new Intent(this, WelcomeActivity.class);
startActivtiy(intent);
It definitely causing you a null pointer exception because your WelcomeActivity is not created yet. You can't start an activity by creating a new instance of Activity with:
WelcomeActivity activity = new WelcomeActivity();
Your activity need to be start with:
Intent welcome = new Intent(context, WelcomeActivity.class);
context.startActivtiy(welcome);
or by adding the correct intent-filter so the activity will be launched by the Android system.
You can start your activity from Application with something like this:
// FLAG_ACTIVITY_NEW_TASK is required
// FLAG_ACTIVITY_CLEAR_TOP brings back to the top of the stack
Intent mainActivity = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mainActivity);
Though I think you should not start an Activity via Application. You need to think throughly about your design so there is no activity connection with the application.
Related
I have an application that has two main activities MainActivity1 and MainActivity2.
I have the Intent filter for the launcher on MainActivity1
activity android:name=".MainActivity1">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
what I want is to be able to switch the launcher activity in my settings activity.
Is that possible and how will it work?
All I did was to create a shared preference boolean the know which activity to open. Then from MainActivity1 I used an intent to go to MainActivity2
Intent intent = getIntent();
boolean firstTimeOpening = intent.getBooleanExtra("BOOLEAN_FIRST_TIME_OPENING", true);
SharedPreferences preferences = getSharedPreferences("SHARED_PREFERENCES", MODE_PRIVATE);
boolean startAppWithActivity1 = preferences.getBoolean(START_APP_WITH_ACTIVITY1, true);
if(!startAppWithActivity1 && firstTimeOpening){ startActivity(new Intent(MainActivity1.this, MainActivity2.class));
}
Easy enough. The firstTimeOpening boolean makes sure the the switching
of activity occurs only when the application is just opened
please help , when i run the app, the app launch a Fatal Error im learning
just i create a instat with a call but not run, (sorry for my english)
.......................................................................
my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.octa.appprueba3">
<uses-permission android:name="android.permission.CALL_PHONE" />
<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" />
<category android:name="android.intent.category.APP_CONTACTS" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
my code:
public class SecondActivity extends AppCompatActivity {
private EditText editPhoneText;
private ImageButton imageCallButton;
private final int PHONE_CALL_CODE = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
editPhoneText = (EditText) findViewById(R.id.editTextPhone);
imageCallButton = (ImageButton) findViewById(R.id.imageCallButton1);
imageCallButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
implicito();
}
});
}
public void implicito(){
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("Tel: 9999999" ));
startActivity(intent);
}
}
You may want to check whether your device can handle your intent by doing this:
PackageManager packageManager = getPackageManager();
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent);
} else {
Log.d(TAG, "Cannot handle this intent");
}
What's more, if you are placing a call like so, there is NO need to declare the permission in your manifest:
<uses-permission android:name="android.permission.CALL_PHONE" />
Because you are not directly calling someone within your app. You are actually transferring the "duty" to other apps which can handle your intent to call. This doesn't need a permission.
To directly make a call within your app, the intent should be Intent.ACTION_CALL, which needs the permission you declared.
Hope this will help.
There is no activity on your device that handles ACTION_DIAL for a Tel: scheme. Try:
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:9999999" ));
Case and whitespace are important when assembling a Uri.
Also note that even my revised Intent will not work on all devices, such as on an Android tablet that lacks a dialer.
I want to start a new activity called Counter when a button is clicked, but I got an error that is the activity is not found...so where is the wrong in my code:
t = new Thread(){
public void run(){
try{
sleep(5000);
}
catch (InterruptedException e){
e.printStackTrace();
}
finally{
Intent counter = new Intent("com.example.test.Counter");
startActivity(counter);
}
}
};
test.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
t.run();
}
});
this is the manifest file :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.test.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="com.example.test.Counter"
android:label="#string/title_activity_counter" >
</activity>
</application>
</manifest>
Intent intent = new Intent(MyActivity.this, OtherActivity.class);
startActivity(intent);
Change this
Intent counter = new Intent("com.example.test.Counter");
startActivity(counter);
This is called implicit intent and it needs intent filter
to
Intent counter = new Intent(MainActivity.this,Counter.class);
startActivity(counter);
This is called explicit intent and there is no need for intent filter
You should use explicit intent coz you have
<activity
android:name="com.example.test.Counter"
android:label="#string/title_activity_counter" >
</activity>
Quoting docs
Explicit intents specify the component to start by name (the
fully-qualified class name). You'll typically use an explicit intent
to start a component in your own app, because you know the class name
of the activity or service you want to start. For example, start a new
activity in response to a user action or start a service to download a
file in the background.
Note: An explicit intent is always delivered to its target, regardless of any intent filters the component declares.
Edit:
You should call start() on the thread not run
You can replace this
Intent counter = new Intent("com.example.test.Counter");
startActivity(counter);
with this, it would work ..
Intent counter = new Intent(MainActivity.this, Counter.class);
startActivity(counter);
Or to let your code work, you are missing intent-filter
<activity
android:name="com.example.test.Counter"
android:label="#string/title_activity_counter" >
<intent-filter>
<action android:name="android.intent.action.COUNTER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
You should always provide an intent-filer if you want to start an activity with your way ..
you should have t.start();. That is the function that starts a thread.
You may also want to try running on the UI thread by replacing the contents of your finally block with this:
MyActivity.this.runOnUiThread(new Runnable() {
public void run() {
Intent counter = new Intent(MyActivity.this, Counter.class);
MyActivity.this.startActivity(counter);
}
};
And as others have said, use t.start() instead of t.run() as run() will block any further action until it completes.
What I am attempting to do is to open "Items" when the ImageButton "ibItem1" is pressed. But, after setting up this:
ibItem1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent items = new Intent("android.intent.action.ITEMS");
startActivity(items);
}
});
And clicking the button does not do anything. I have the activity all set up in the manifest:
<activity
android:name="com.example.custombuilds.Items"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.ITEMS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
The program has crashed before because of a ActivityNotFoundException, even though I declared it in the manifest.
Intent myIntent = new Intent(this, NextActivity.class);
startActivity(myIntent);
In this form, it should work..
Also make sure that the name of the class that implements the activity is the same in the manifest file at the android:name="atributevalue".The attribute value should be a fully qualified class name.
I have 2 Activitys
MainActivity
LoginActivity
I want verify if user has logged, else show my LoginActivity before the MainActivity apears.
When I put this in OnCreateView of MainActivity I have an error:
if(Login.isLogged()){
selectItem(0);//starts the fragment
}
else {
Intent i = new Intent(MainActivity.this, LoginActivity.class);
startActivity(i);
}
FATAL EXCEPTION: MAIN java.lang.RuntimeException: Unable to start activity ComponentInfo { .....MainActivity}: java.lang.NullPointerException
Ps: My MainActivity is an SherlockFragmentActivity. I'm using DrawerLayout.
How can i start my LoginActivity before SherlockFragmentActivity?
Thanks
If I were you I would put LoginActiviy as launcher activity. There I would check if user has already logged in, if he has start new activity.. Something like this:
//in onCreate of LoginActivity
if (hasLogedIn) {
Intent i = new Intent(LoginActivity.this, MainActivity.class);
startActivity(i);
finish();
} else {
// show him login activity, setContentView etc...
// save hasLogin boolean as true
}
make this changes in your AndroidManifest file
<activity
android:name="com.example.alphabets.LoginActivity"
android:label="#string/title_activity_login"
android:windowSoftInputMode="adjustResize|stateVisible" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.alphabets.MainActivity"
android:label="#string/app_name" >
</activity>
Make Login Activity as your default activity. Check Login status. If user is logged in, call Main Activity and finish() the Login Activity. else, wait for user input