I have been reading Google's android tutorial and I got a problem... In the tutorial they explain the XML elements (EditText and Button) and in the end they say how to tun the program to see the button + the text field.
The problem is, that the emulator doesn't show them.. just a black screen.
I even tried adding this line to the onCreate function -
System.out.println("Hello World!");
but still the emulator is showing only black screen..
Here's the .java main file - http://pastebin.com/G5J9YjNe
And the XML files (I mentioned what code is what file) - http://pastebin.com/VnRwAfMW
What should I do?
Thanks a lot for everyone who will help!
System.out.println("Hello World!");
does not print any thing on screen. it can seen on Consol. i tried below code and working fine for me.
// Main Activity
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.kuchbhi);
System.out.println("Hello World!");
}
Layout /.xml
<LinearLayout 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"
android:orientation="horizontal" >
<EditText android:id="#+id/text_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="enter text" />
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Send" />
</LinearLayout>
Manifest file ..
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<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>
</application>
</manifest>
try this code and let me know what u see.
I tried your source code and it is working fine for me. see screen
Related
My bare-bones test app sets the orientation to landscape on start up. It has a layout and layout-port directory. There is a simple layout file in each directory. They contain a textview with text set to either "Landscape" or "Portrait".
In my test I start the app on a tablet tipped into portrait. With this code, I would expect the layout from /layout/my_layout.xml to be used, but /layout-port/my_layout.xml is used instead. I've also tried making a /layout-land directory but still no luck.
From my main activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Start app with tablet in portrait orientation
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
int o = this.getRequestedOrientation();
// o is 0, indicating landscape. Display shifted to landscape
setContentView(R.layout.my_layout);
// layout-port/my_layout.xml is loaded. INCORRECT I think
}
/layout/my_layout.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:background="#ff000000">
<TextView
android:id="#+id/tvLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Landscape"
android:textSize="50sp"
android:textColor="#ffffffff"/>
</LinearLayout>
/layout-port/my_layout.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:background="#ff000000">
<TextView
android:id="#+id/tvLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Portrait"
android:textSize="50sp"
android:textColor="#ffffffff"/>
</LinearLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mytest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.mytest.ActivityMain"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/app_name"
android:windowSoftInputMode="stateHidden|adjustPan" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Change /res/layout/my_layout.xml to /res/layout-land/my_layout.xml
Note: I added the suffix -land
Your have to remove option "orientation" in manifest:
android:configChanges="screenSize|orientation|keyboardHidden"
Because this option says you handle it by yourself...
Good explanation here:
Hi guys i have this splash screen with the layout main.xml which contains an imageview,
here is the main.xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/slide11" />
</LinearLayout>
and here is my splashscreen.class file
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
new Handler().postDelayed(new Runnable() {
public void run()
Intent intent = new Intent(getApplicationContext(),content_activity.class);
startActivity(intent);
Main.this.finish(); } }, 5000);
} catch(Exception e){}
}
#Override
public void onBackPressed() {
super.onBackPressed();
} }
when i try to run this in my emulator all works fine, but when i try to run it in a device through debugging mode i don't get the image specified in the imageView, but i get a white screen for the specified amount of time.any help would be much appreciated.
//edit : i double checked the res/drawable folder and i tried primarily using png and also gave a go using .gif none worked in the device. (device micromax a110)
use this instead of your layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/slide11" >
looks like it was because of the size of the image, when reduced worked !!
Use image as layout background :-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/slide11"
>
</LinearLayout>
This can be happening due to issue in the manifest file. Here is an example of my manifest file which has 3 activities and 1 splash screen. Hoping that this helps out.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.foodorder">
<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/Theme.FoodOrder">
<activity android:name=".MainActivity"></activity>
<activity android:name=".Final_activity" />
<activity android:name=".Second_activity" />
<activity
android:name=".SplashActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I have written an app with XML and still I don't write Classes by java code. But the emulator can show my app by its name and after that it says " unfortunately, app has stopped." What I have to do for this problem??!
This is my code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Textview
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/TextView2"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/Button"
/>
</LinearLayout>
You must have at least one activity (Java class) in your application so that manifest.xml could recognize that and run it.
You have to use an activity to show this xml :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
As you can see your xml is called by the setContentView method.
This activity must be declared in your AndroidManifest.xml with
<activity
android:name="YOURPACKAGE.ACTIVITY_NAME"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I am new to Android SDK,but I know JAVA a little.I am on SDK v4.The problem is that,I tried to make 'HelloWidget' Home Screen Widget (with just a text box),and when I try to run the project,the console gives me
No Launcher Activity Found!
The application will only sync the package to the device!
And my app is nowhere to be found in the AVD.I had tried putting the .MAIN and Launcher in Intent filter,but no good.Here are my files:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ax.startup"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<!-- Broadcast Receiver that will process AppWidget updates -->
<receiver android:name=".AxStartup" android:label="AxStartup">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="#layout/startup_info" />
</receiver>
</application>
</manifest>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="HELLO WORLD" />
</LinearLayout>
startup_info.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" >
<EditText
android:id="#+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:text="ITS WORKING DUDE!"
android:minWidth="146dip"
android:minHeight="72dip"
android:updatePeriodMillis="10000"
android:initialLayout="#layout/startup_info">
<requestFocus />
</EditText>
</LinearLayout>
Java file
package ax.startup;
import android.appwidget.AppWidgetProvider;
public class AxStartup extends AppWidgetProvider {
}
I followed the tutorial from here:
http://www.helloandroid.com/files/xmaswidget/android_howto-hellowidget.pdf
And another strange problem is that when I long press on desktop,only options to change wallpaper is coming.There is no 'Add to Home Screen' or 'Widgets' or any other menu!!!
Please tell me what is the problem.
An application widget can not be launched with a MAIN action, only activities. Also, if your app only has an application widget you will not see an app icon. The log messages are saying that the app was deployed to the emulator but you must start the app widget manually.
Go to the home screen on the device and do a long-press on the screen. You will get a menu to add an element to the home screen. One of the options will be "Widgets". Select that and navigate to your app widget. Your app widget will now be on the home screen.
Trying to get an Android 2.2 application to start up with a SurfaceView as the base view with a button placed atop it located near the bottom of the screen. So far, no luck. It crashes every time it attempts to launch. I've checked to make sure that the activity is registered in the manifest.
Here's my java code:
public class Dragable extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
and here's my main.xml:
<?xml version="1.0" encoding="utf-8"?>
<SurfaceView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/surface_home"
>
<Button
android:id="#+id/add_new"
android:text="#string/add_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</SurfaceView>
and my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Dragable"
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>
<uses-sdk android:minSdkVersion="8" />
</manifest>
and here's my error:
11-29 11:58:52.620: ERROR/AndroidRuntime(512): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.Dragable}: java.lang.ClassCastException: android.view.SurfaceView
Can't seem to find anything related doing searches on SO. My appologies if this has been asked before.
Now I see the problem. SurfaceView is not a container, I mean it does not extends ViewGroup, so you can't put a Button inside a SurfaceView. What you can do is wrapping the SurfaceView and the Button within a RelativeLayout.
You are not using any container to place components. Use this and make changes
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SurfaceView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/surface_home"
>
<Button
android:id="#+id/add_new"
android:text="#string/add_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
/>
</SurfaceView>
</FrameLayout>