yesterday i created a Desk clock for android. For this I created a RelativeLayout to show Hours,minutes,etc... and a menu to show the options About, and Exit...Well, all works fine, but the problem comes when I click About Option: it force Closes and LogCat shows this:
E/AndroidRuntime(14751): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.iamaner.T2Speech/com.iamaner.T2Speech.FrmAbout}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
So I hope you can help me...
Ive heard about this code, but i dont know where to put it,and how, i thought this would be Ok but it seems not
final RelativeLayout fondo = (RelativeLayout)findViewById(R.id.your_layout);((RelativeLayout)fondo.getParent()).removeView(fondo);
//scrollView.removeView(scrollChildLayout);
Here is the manifest file i think its the problem, i dont know:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TimeToSpeechActivity"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:theme="#android:style/Theme.Light.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FrmAbout" />
</application>
And here, the FrmAbout code:
public class FrmAbout extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.frmabout);
TextView txtVersion = (TextView)findViewById(R.id.Txtversion);
TextView txtWarning = (TextView)findViewById(R.id.Txtwarning);
TextView txtInstructions = (TextView)findViewById(R.id.Txtinstructions);
TextView txtContact = (TextView)findViewById(R.id.Txtcontact);
setContentView(txtVersion);
setContentView(txtWarning);
setContentView(txtInstructions);
setContentView(txtContact);
}
}
Ok, solved it.... the problem was the setcontentview() method, deleted it and now it works
From your code i didn't understood Why you removing all views before calling About activity? Is this any of your requirement because to start new activity all the views need not be removed.. You can directly call startActivity(intent) without removing views from current activity.
Please post logs to help understand the problem.
Related
I'm an Android noob and I'm having difficulty finding out something basic about Android.
I currently have an activity_main.xml file.
I want to use this layout when I first start the Android emulator, using Android Studio
Does Android look for a file activity_main.xml and use it as the default layout?
Here's my 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>
</application>
So I understand that this specifies that my .MainActivity will respond to an action.MAIN intent call. What I don't know is what the action.MAIN intent call actually is, and how my activity_main.xml relates to this.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Can anyone provide an explanation or a link to a good primer that explains these basic concepts?
From manifest :
<action android:name="android.intent.action.MAIN" />
Means this activity is the entry point of the application. In your case, the MainActivity starts.
The MainActivity sets up the layout for itself - the line responsible is
setContentView(R.layout.activity_main);
Where it looks for layout file activity_main.xml. This is just the naming convention - feel free to rename the layout file and call the new one from setContentView. It's not required to be called activity_main
I have the following Androidmanifest file
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
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=".First"
android:label="#string/title_activity_first"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="android.anoop.com.myapplication.MainActivity" />
</activity>
<activity
android:name=".Second"
android:label="#string/title_activity_second"
android:parentActivityName=".First" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="android.anoop.com.myapplication.First" />
</activity>
</application>
In main activity's onCreate method i have the following code to launch Second activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent1=new Intent(this,Second.class);
startActivity(intent1);
}
Other than that every other activities code is what android studio provide as default. But when i press up button from the Second activity it goes directly to main activity and not to the First activity. I know there are alternate solutions for this problem but i just want to know why the up navigation is behaving like this.I am new to android so i may not be understanding something really simple. Any help will be appreciated. Thank you.
If you are using calling SecondActivity in onCreate of FirstActivity, then it goes to SecondActivity. But think about it, when do an Activity actually starts?
It starts after you call onCreate, then onStart , then onResume and then your Actiivity finally starts running. In your case, you are not allowing to go further onCreate, and hence FirstActivity has never even started. And thats why when you go back in navigation it jumps to the previously opened Activity, i.e., MainActivity.
I hope this clears your answer! For reference see the Lificycle of an Activity in this link.
Because FirstActivity is not started or Created. You are calling Second directly from Main Activity. As there is no any First Activity available in application's stack how Second Activity goes to parent Activity First? It will goes to Main Activity as Its the only Activity remain in stack.
Hi stackoverflow friends. I have an player music and I call in oncreate playSong(id)(id is index of an array which has a path music in that index of array.But there is this problem that when rotates Screen then songs over and over plays from first index in array. I guess becuase oncreate method is called after each time event rotation. Also I want my app prevent from rotation becuse may be this event have bad effect in others activity.too,
How I can fix these type of issues?
You can use
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
or
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
to lock the orientation. Because as you mentioned, the onCreate is called everytime the screen rotates.
But you have to call it before the setContentView(R.layout.main)
Could look like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.main);
}
you can do like this android:configChanges="orientation|keyboardHidden|screenSize"
hope it will help you
add it to manifest as follows as an example
<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"
android:configChanges="orientation|screenSize|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
You could add android:configChanges="orientation" to Activity tag on AndroidManifest.xml. Try it.
You could add android:configChanges="orientation|screenSize" to Activity tag on AndroidManifest.xml. It will helpful.
I'm writing simple APN toggle app. I wanted to ask how to force android not show any window.
Currently after running my app, for brief time Black screen with app name is shown and then disappears.
Is it possible not to show anything ( only Toast message ) ?
public class ApnSwtichActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (toggleAPN()){
Toast.makeText(this, "Apn switched", Toast.LENGTH_SHORT).show();
}
this.finish();
}}
Sounds like you want an activity without a UI
How to launch an Activity without a UI?
You'll probably want to use
android:theme="#android:style/Theme.NoDisplay"
I don't think this is possible exactly as you want. You can hide title bar like this: add android:theme="#android:style/Theme.Light.NoTitleBar.Fullscreen" in your manifest:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".TestActivity"
android:theme="#android:style/Theme.Light.NoTitleBar.Fullscreen"
>
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You could create an activity, make it transparant and give it the parameters FLAG_NOT_TOUCH_MODAL & FLAG_NOT_TOUCHABLE
(FLAG_NOT_TOUCH_MODAL passes any touch input you give to your activity to the underlying screen, FLAG_NOT_TOUCHABLE negates any touch input commands for the activity)
Don't forget to make your activity autoclose after you're done
Hey, I realize there are tutorials on this topic, and even previous questions posed. However, I've read several tutorials and some answers and I am still having trouble. Clearly, I must not be the brightest crayon in the box.
My program crashes when I try to switch between activities with the following code:
final Button switchButton = (Button) findViewById(R.id.change_mode);
switchButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent runOptionSelect = new Intent(TheDecider.this, OptionSelect.class);
startActivity(runOptionSelect);
return;
}
});
I think this code is fine so it must be an issue with the manifest.xml right? I don't understand when to use which activity constant. If my purpose is simply to switch to a different layout and class, what should I choose?
Also, are MAIN and LAUNCHER only to be used on the initial activity to be run?
So sorry for asking such a basic question but I've spent far too much time researching this to no avail. Thank you.
Please check below code in your manifest.xml file
<activity android:name=".TheDecider"
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=".OptionSelect"
android:label="#string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
In the intent, the first parameter is the current context (you can do
TheDecider.this
or
getApplicationContext()
there) and the second one is the class from the activity you are trying to reach.
You are doing that right. And in your manifest you should add
<activity android:name=".OptionSelect"
android:label="#string/app_name" />
You have to add EVERY Activity in your Manifest, otherwise it will crash. Without knowing your logcat's content, that's all i can say.