Unable to launch a new activity as main activity - android

Initially my Android project had only one activity named MainActivity. Then, I added a new activity named ChildActivity into my project. I'm trying to launch my new activity as startup activity when app gets launched. I changed AndroidManifest.xml file to achieve it. I moved the entire intent-filter tag from MainActivity to ChildActivity as shown below:
<activity android:name=".MainActivity">
</activity>
<activity android:name=".ChildActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Layout file of ChildActivity looks like this. I'm trying to show a TextView on the new activity I've just added:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ChildActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_display"
android:text="Hello this is Rasik!"/>
</android.support.constraint.FrameLayout>
ChildActivity.java file:
package com.example.android.explicitintent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class ChildActivity extends AppCompatActivity {
private TextView mDisplayText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child);
}
}
Whenever I try to debug my app in Android AVD, nothing happens. It seems my app is crashing at start up. Can anyone help me in diagnosing the root cause?

The class android.support.constraint.FrameLayout not exists. Try LinearLayout, FrameLayout, android.support.constraint.ConstraintLayour or others

please try this !
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_display"
android:text="Hello this is Rasik!"/>
</FrameLayout>
i think there is issue with root framelayout

You used wrong layout.
Just convert android.support.constraint.FrameLayout to FrameLayout.
Or convert to default android.support.constraint.ConstraintLayout
<FrameLayout
...>
<TextView
...
/>
</FrameLayout>

The Root layout android.support.constraint.FrameLayout you are using does not exist. Check out this link Android Developer guide.
Your Root layout can be:
FrameLayout
LinearLayout
RelativeLayout
ConstraintLayout
There are many layouts available which you can use based on your need.

I had seen a similar issue in another post. Just off the bat, it seems like your child activity is not within the enclosing terms of the ".MainActivity."
Put the child activity into the brackets of the ".MainActivity", in between the
<activity android:name=".MainActivity"> </actvitiy>. Hopefully that makes it run better. If that doesn't work, start the child activity during the onCreate() method of the MainActivity.

Related

Default Activity not found.. how is default activity defined?

How exactly is default activity defined?
As far as I can tell, adding "andriod.intent.action.MAIN" to AndroidManifest.xml like this:
<application
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN/>
That should define .MainActivity as the default activity, but it's not working. I've also tried adding the full package name com.package.MainActivity but that's also not working.
This is probably caused by my layout setup, I'm trying to define a layout dynamically in code... here's my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/mainlao">
</LinearLayout>
Then I define my TextureView layout in onCreate here:
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
LinearLayout layout = (LinearLayout) findViewById(R.id.mainlao);
mButton = new Button(this);
mButton.setText("pic");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
mTextureView = new TextureView(this);
mTextureView.setSurfaceTextureListener(this);
layout.addView(mTextureView);
layout.addView(mButton, lp);
setContentView(layout);
}
I can't see how my layout would effect it, but this is the first time I've had this error, and the first time I tried doing a layout like this. No errors in code, and I tried invalidating caches.
Google hasn't turned up much yet, except for that android.intent.action.MAIN.. any help is appreciated.
Let me know if you need more information about the app.
The activity's category should be of type 'Launcher'
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN/>
<category android:name="android.intent.category.LAUNCHER" /> // This
</intent-filter>
</activity>

Genymotion VMs display Theme.Holo.DialogWhenLarge-themed activities as normal ones

I'm developing an Android tablet application containing lots of activities that use the Theme.Holo.DialogWhenLarge theme. On real devices (Nexus 7 KitKat and Samsung Galaxy Tab 3 8") these activities are displayed as expected, but on every Genymotion VM I try to create (Nexus 7 KitKat and Nexus 10 Jelly Bean for example) they are displayed as normal full-screen activities. I tested also on a Nexus 7 KitKat emulator created with the standard Android SDK Virtual Device Manager, and activities are displayed as expected.
I can ignore it and suppose that it's only a problem with the Genymotion emulator, but I want to know if there's a way to fix that in order to have a better test environment. I would like to point out that the *-large resource qualifier works on the Genymotion VMs, the issue looks related only to that theme.
To reduce all possible causes of the problem I created a new test project with a normal main activity having a button that launches a DialogWhenLarge-themed activity. I also removed all Android support libraries and all XML styles files automatically created by Eclipse: the only resources left are the two layouts for the activities and the launcher icon. These are all the files of the test project:
src/com/example/testdialogwhenlarge/MainActivity.java
package com.example.testdialogwhenlarge;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
// Start the DialogWhenLarge-themed activity
startActivity(new Intent(MainActivity.this, TestActivity.class));
}
});
}
}
src/com/example/testdialogwhenlarge/TestActivity.java
package com.example.testdialogwhenlarge;
import android.app.Activity;
import android.os.Bundle;
public class TestActivity extends Activity {
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
}
res/layout/activity_main.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.testdialogwhenlarge.MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</FrameLayout>
res/layout/activity_test.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.testdialogwhenlarge.TestActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</FrameLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testdialogwhenlarge"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="TestDialogWhenLarge"
android:theme="#android:style/Theme.Holo">
<activity android:name="com.example.testdialogwhenlarge.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.testdialogwhenlarge.TestActivity"
android:theme="#android:style/Theme.Holo.DialogWhenLarge">
</activity>
</application>
</manifest>
This issue has been fixed by the Genymotion team in release 2.3.0.

Having Trouble generating R.Java

I'm recently been trying to get R.JAVA to generate with no avail. I've tried the following
1. Adding spaces to the manifest file
2. Project--> Clean
So my last guess is that there is some error in my xml files that I/eclipse haven't been able to see. So I was hoping to have a second pair of eyes that might be able to check my xml files for review. There's only three and they should be pretty basic(I'm following a intro to android book).
Manifest File
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activites"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity2"
android:label="Activity 2" >
<intent-filter>
<action android:name="com.example.ACTIVITY2" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>
Activity2.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is Activity 2!" />
</RelativeLayout>
Again thanks to all of you that have taken time out of your busy day/night to help out a beginner like myself. Also if there's any suggestion other than errors in the xml feel free to bring them up.
As requested here are the problems appear in windows-> show view -> problems
R cannot be resolved to a variable
R cannot be resolved to a variable
R cannot be resolved to a variable
Tag is not that obvious it is used to show debug information in catlog with the following lines of code.
public class MainActivity extends Activity {
String tag = "Events";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Log.d(tag, "In the onCreate() event");
}
In eclipse you can check the Problems view(windows-->show view-->Problems) or/and the Console view to help locate errors in Xml efficiently. If you remain need help you may want to post these errors to us
I get following error when I compile your code:
res\layout\Activity2.xml: Invalid file name: must contain only
[a-z0-9_.]
So remove capital A and rename "Activity2.xml" to "activity2.xml"
Also, if this doesn't help, try this:-
Rename your package name to some other. Clean. Restart Eclipse. Rename package back to the previous one. Again clean. Restart Eclipse.
check your imports. is import android.R anywhere there? if so, remove it. if you're indeed following some android tutorial in a book and it instructed you to organize or manage imports with Ctrl+shift+O or otherwise, that will cause that to pop up.
tag should be a String variable. What is it defined as? You should probably hard-code it there, unless you are using it as a global variable for your application.
http://developer.android.com/reference/android/util/Log.html#d%28java.lang.String,%20java.lang.String%29
EDIT: R cannot be resolved - Android error has a lot of different answers, try reading through those. If the first one doesn't solve your problem, another one might.

Layout does not appear in android emulator

I'm new to Android programming and i'm doing tutorial from layouts from http://developer.android.com/resources/tutorials/views/index.html . Relative layout tutorial exactly. I did everything they say but when I try to start app in android emulator there are no buttons, textfields etc. Only name of app shows at the top. What is wrong? Is this a problem with emulator? I'm using Eclipse Version: 3.7.1.
EDIT:
package pchot.tutorial;
import android.app.Activity;
import android.os.Bundle;
public class Tutorial1Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pchot.tutorial"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Tutorial1Activity"
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>
Layout is copy/paste from http://developer.android.com/resources/tutorials/views/hello-relativelayout.html
EDIT:
Ok, problem solved. I needed to delete "Hello World, Tutorial1Activity!" from string.xml file which was autocreated during creation of project in Eclipse.
Have you modified you manifest.xml file? Maybe you haven't modify it to make it aware that the launched main activity is the activity you've just created.
You didn't declare anything in your onCreate() method.
For Button declare Button btn = (Button)findViewById(R.id.btnSubmit)
Implement onClickListener if you want the button to listen to clicks.
For TextView, declare TextView tv = (TextView)findViewById(R.id.textview)

Getting Blank Screen when Calling StartActivity()

Thanks in advance for your efforts.
I have an Application that revolves around a ListActivity extended class. Before the app starts, I want to check whether the user is registered, and if not tell him to and get some info from him. So, I tried to call StartActivity in the OnCreate() method. When that loaded, I got a big black screen.
I thought that it may be related to being run in the OnCreate so I let the Activity start as usual, and I tried to run it in an OnClick event, and I got the same result.
In both cases, if I press escape, that Window goes away and the main app window comes back.
Here are the lines from where I call the new Activity
Intent emailIntent = new Intent(this, EmailAddressGetter.class);
this.startActivity(emailIntent);
Here is the code of the class
/**
*
*/
package com.kosherapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
/**
* #author Josh
*
*/
public class EmailAddressGetter extends Activity {
public void OnCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.emailinput);
}
}
Here is the emailinput xml contents
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffff"
android:textColor="#000000"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:text="hello"
>
</TextView>
And, here is the manifest contents
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kosherapp"
android:versionCode="1"
android:versionName="1.0"
>
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission
android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.CALL_PHONE" />
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
>
<activity
android:name=".KosherApp"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar"
>
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".EmailAddressGetter"
android:label="email Address Getter"
>
</activity>
<activity
android:name=".GoogleMaps"
android:label="Google Maps"
>
</activity>
<uses-library
android:name="com.google.android.maps" />
</application>
</manifest>
Let me know if there's any other info you may need. Oh, I'm running this with the Google API 2.1-update
Thanks
Change OnCreate to onCreate in EmailAddressGetter.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.emailinput);
}
You need to use EditText instead. TextView is for static text which is why nothing is displaying. You should also include the EditText in another layout like Linear or Relative so it looks cleaner.
You are using an intent to call EmailAddressGetter but it doesn't have an intent filter in the manifest. Throw in something like Log.e("EmailAddressGetter", "It worked") in your onCreate and use ddms to see if it is actually opening that activity.
Also, in the updated code in your comment to Robby Pond, remove the xmlns line from EditText

Categories

Resources