Activity not started - android

In my OnClick event attached to an imageview, I want to open a new activity with a screensized imageview on it to show the picture.
This is the event:
public void onClick(View arg0) {
PhotoList pli = (PhotoList) arg0.getTag();
Intent i = new Intent(getBaseContext(), PhotoActivity.class);
i.putExtra("photo", pli.Photo);
i.putExtra("PhotoId", pli.id);
getBaseContext().startActivity(i);
}
This code is being executed when I click on the image, but nothing happens.
The onCreate of PhotoActivity (which is never executed):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo);
getActionBar().setDisplayHomeAsUpEnabled(true);
Bundle extras = getIntent().getExtras();
photo = extras.getByteArray("photo");
ViewId = extras.getInt("View");
PhotoId = extras.getInt("PhotoId");
if (photo.length > 0) {
ImageView iv = (ImageView) findViewById(R.id.pvPhoto);
Bitmap mBitmap = BitmapFactory.decodeByteArray(photo, 0, photo.length);
iv.setImageBitmap(mBitmap);
}
The manifest:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="enn.com.sitefinder.MainActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PhotoActivity"
android:label="#string/title_activity_photo" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="enn.com.sitefinder.MainActivity" />
</activity>
</application>
When I accidently connected the OnClick eventlistener to the linear layout, the activity did start.
Why is the activity not shown?

Solved it by passing on a extra integer in the extras, allowing the PhotoActivity class to find the image in the arraylist.
This instead of passing the bitmap byte array which is too much for the bundle.

you do not need to use getBaseContext.startActivity
just use startActivity(intent);

Related

No Activity found to handle Intent

I know that there is a lot of questions related to this issue, but none of them solve my problem. I made a quiz app with 50 questions fragments. In the final question (Question 50 fragment) I call the score Activity to show the score. In this Activity I created a "Play Again" button, which calls the Main Activity again to start the game. But everytime I click on "Play Again" button, my app crashes.
Logcat error: 04-03 15:34:43.638 26316-26316/com.example.moresche.englishqigame E/AndroidRuntime: FATAL EXCEPTION: main
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.moresche.englishqigame.MainActivity }
scoreActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_score);
Typeface mTypeface = Typeface.createFromAsset(getAssets(),"chlo.TTF");
TextView myTextview = (TextView)findViewById(R.id.txtla);
myTextview.setTypeface(mTypeface);
Typeface m2Typeface = Typeface.createFromAsset(getAssets(),"chlo.TTF");
TextView m2yTextview = (TextView)findViewById(R.id.txtla1);
m2yTextview.setTypeface(m2Typeface);
initControls();
}
public void initControls() {
TextView final_score = (TextView) findViewById(R.id.textView103);
TextView final_scoreqi = (TextView) findViewById(R.id.textView104);
TextView lvlqi = (TextView) findViewById(R.id.textViewzzz);
Button btnxd = (Button)findViewById(R.id.btnx);
final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
...
btnxd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
app_preferences.edit().clear().commit();
Intent intent1 = new Intent ("com.example.moresche.englishqigame.MainActivity");
startActivity(intent1);
}
});
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.moresche.englishqigame">
<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"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".scoreActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.example.moresche.englishqigame.scoreActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
You are calling your intent in the wrong way.
Intent intent1 = new Intent ("com.example.moresche.englishqigame.MainActivity");
It should be something along the lines of:
Intent intent1 = new Intent(scoreActivity.this,MainActivity.class);
http://developer.android.com/training/basics/firstapp/starting-activity.html
Although that will only taking you to the page not play the game again.

2nd Activity won't open when Launching Activity is in Background

I have two relevant activities: Main and ShowResult. The 2nd one is launched from a Thread in Main. This works well, but as soon as Main is in background the activity won't open. Logcat doesn't show any anomalies.
Manifest:
<activity
android:name="com.....ShowResultActivity"
android:launchMode="singleTop"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen" >
</activity>
<activity
android:name="com......MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:theme="#android:style/Theme.Holo.Wallpaper.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Handler inside the Thread-class / executed from Thread:
iOpen = new Intent(context, ShowResultActivity.class); // is written in the constructor
Bundle b = new Bundle(); // global
b.putInt("type", 1);
b.putString("url", value);
iOpen.putExtras(b);
context.startActivity(iOpen);
ShowResult:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
}
#Override
public void onNewIntent(Intent i) {
Bundle b = i.getExtras();
int value = b.getInt("type");
String url = b.getString("url");
Log.e("opened :)", value+" "+url);
if(value == 0) {
showPictureAsync(url);
} else if (value == 1) {
showVideoAsync(url);
}
}
Add this to your code
iOpen.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(iOpen);

Android - MainActivity keeps on instantiating

Im very new to android development so please bear my ignorance.
I created a Splash screen before loading my main activity. Splash is working fine but what causes the problem is the main activity, it keeps on instantiating.
Splash.java
public class Splash extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
final AdController optinController = new AdController(
getApplicationContext(), "SECTION_ID");
final Splash splash = this;
optinController.loadOptin((Activity) splash, "SECTION_ID",
new AdOptinListener() {
public void onAdOptin() {
// once optin process is complete, continue to main app activity
launchMain();
}
}
);
}
public void launchMain() {
finish();
Intent myIntent = new Intent(Splash.this, MainActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(myIntent);
}
}
MainActivity.java
public class MainActivity extends Activity {
private AdController myController;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myController = new AdController(getApplicationContext(), "SECTION_ID");
myController.loadNotification();
}
}
In the manifest file:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
android:label="#string/title_activity_splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I used android:launchMode="singleInstance" but still it keeps on reinstantiating.
Please help. Thanks in advance.
public class Splash extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//|Window.FEATURE_INDETERMINATE_PROGRESS
setContentView(R.layout.splash);
final Timer time = new Timer();
time.schedule(new TimerTask() {
#Override
public void run() {
Intent intent = new Intent(Splash.this,MainActivity.class);
startActivity(intent);
finish();
}
},1000);
}}
It will work , i din get you exactly by instatiating.Also do no use single instance,you are not supposed to require it here.
Yes, following is my snippet of manifest file , just use a timer for your splash screen and move to your main class after specific time.
activity android:name=".Splash"
android:screenOrientation="portrait" android:label="#string/app_name">
i think you have to remove the intent flag Intent.FLAG_ACTIVITY_SINGLE_TOP
In manifest.xml, set category as LAUNCHER which activity you want to launch first and to Other set as DEFAULT as given in below example.
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
android:label="#string/title_activity_splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>

Nothing happens on startActivity()

I have 2 activities with a few buttons etc. I want to start new activity. I have done everything the tutorial said, but the 2nd activity does not start!
case R.id.btnIstorija:
Intent i = new Intent (this,KlasaPrikazBaze.class);
startActivity(i);
break;
It should start my 2nd activity
public class KlasaPrikazBaze extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.prikazbaze);
TextView TV = (TextView)findViewById(R.id.tvSQLinfo);
KlasaBaze info = new KlasaBaze(this);
info.open();
String podatak = info.DohvatiPodatak();
info.close();
}
}
However nothing happens.
Manifest:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".TalentiFinalActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".KlasaPrikazBaze"></activity>
</application>

How to change title of Activity in Android?

I am using
Window w = getWindow();
w.setTitle("My title");
to change title of my current Activity but it does not seem to work.
Can anyone guide me on how to change this?
Try setTitle by itself, like this:
setTitle("Hello StackOverflow");
Just an FYI, you can optionally do it from the XML.
In the AndroidManifest.xml, you can set it with
android:label="My Activity Title"
Or
android:label="#string/my_activity_label"
Example:
<activity
android:name=".Splash"
android:label="#string/splash_activity_title" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
If you want it one time & let system handle the rest (not dynamic) then do like this in your manifest file:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name_full" > //This is my custom title name on activity. <- The question is about this one.
<intent-filter android:label="#string/app_launcher_name" > //This is my custom Icon title name (launcher name that you see in android apps/homescreen)
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
setTitle(getResources().getText(R.string.MyTitle));
There's a faster way, just use
YourActivity.setTitle("New Title");
You can also find it inside the onCreate() with this, for example:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setTitle("My Title");
}
By the way, what you simply cannot do is call setTitle() in a static way without passing any Activity object.
This worked for me.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment, container, false);
getActivity().setTitle("My Title");
//...
}
If you have multiple activities, you can set it like this in AndroidManifest.xml
<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=".NumbersActivity"
android:label="#string/category_numbers"
android:theme="#style/category_numbers" />
<activity
android:name=".FamilyActivity"
android:label="#string/category_family"
android:theme="#style/category_family" />
<activity
android:name=".ColorsActivity"
android:label="#string/category_colors"
android:theme="#style/category_colors" />
<activity
android:name=".PhrasesActivity"
android:label="#string/category_phrases"
android:theme="#style/category_phrases" />
<activity
android:name=".ExperimentActivity"
android:label="#string/category_experiment"
android:theme="#style/category_experiment" />
</application>
I'm using Android Studio 3.0.1.
WIth an Activity:
setTitle("Title Text");
Inside a fragment:
getActivity().setTitle("Title Text");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Main_Activity);
this.setTitle("Title name");
}
If you want to set title in Java file, then write in your activity onCreate
setTitle("Your Title");
if you want to in Manifest then write
<activity
android:name=".MainActivity"
android:label="Your Title" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
In Kotlin, this way:
this.title = resources.getText(R.string.fast_learning)
I have a Toolbar in my Activity and a Base Activity that overrides all Titles. So I had to use setTitle in onResume() in the Activity like so:
#Override
protected void onResume() {
super.onResume();
toolbar.setTitle(R.string.title);
}
The code helped me change the title.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name);
ActivityName.this.setTitle("Your Activity Title");}
Inside a MainActivity:
public class act1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act1);
setTitle("First Activity");
}
}
setTitle("Whatever apps");
in MainActivity.java is simplier
I would say.
If you want to change Title of activity when you change activity by clicking on the Button. Declare the necessary variables in MainActivity:
private static final String TITLE_SIGN = "title_sign";
ImageButton mAriesButton;
Add onClickListener in onCreate() and make new intent for another activity:
mTitleButton = (ImageButton) findViewById(R.id.title_button);
mTitleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,
SignActivity.class);
String title_act = getText(R.string.simple_text).toString();
intent.putExtra("title_act", title_act);
startActivity(intent);
finish();
}
});
SecondActivity code in onCreate():
String txtTitle = getIntent().getStringExtra("title_act");
this.setTitle(txtTitle);
If you're using onCreateOptionsMenu, you can also add setTitle code in onCreateOptionsMenu.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
setTitle("Neue Aktivität");
return true;
}
setTitle("Welcome Activity");

Categories

Resources