error in running application on android emulator - android

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>

Related

Button ClickListener crashes app

I'm trying to make an application that has multiple tabs, and for now I only have the tabs to worry about. The problem I have is that, when I uncomment the button setOnClickListener it gives me an error when trying to run it on the AVD.
Here is my main function
`
#Override
public void onCreate(Bundle Bgilca) {
Button blade;
super.onCreate(Bgilca);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
launchMusic = MediaPlayer.create(main.this, R.raw.powerup);
launchMusic.start();
Thread launchTime = new Thread(){
public void run(){
try{
sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
launchMusic.pause();
}
}
};launchTime.start();
}
Button blade = (Button)findViewById(R.id.fantab);
}`
when trying to execute in the AVD, the app crashes giving the message"unfortunately app_name has stopped"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bgilcag60"
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="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".main"
android:label="#string/app_name"
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=".ventscreen"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.example.bgilag60.ventscreen" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
'
here is my Layout.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"
android:background="#drawable/background"
android:keepScreenOn="true"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
tools:context="com.example.bgilcag60.main" >
<Button
android:id="#+id/scaunstg"
android:layout_width="170dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#drawable/scaunstg"
android:baselineAlignBottom="true"
android:clickable="true" />
<Button
android:id="#+id/scaundr"
android:layout_width="170dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/scaundr"
android:baselineAlignBottom="true"
android:clickable="true" />
<Button
android:id="#+id/home"
android:layout_width="160dp"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:background="#drawable/corrado_home"
android:clickable="true" />
<Button
android:id="#+id/head"
android:layout_width="66dp"
android:layout_height="80dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/head_tab"
android:clickable="true" />
<Button
android:id="#+id/navi"
android:layout_width="66dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_marginRight="95dp"
android:layout_toLeftOf="#+id/head"
android:background="#drawable/navi_tab"
android:clickable="true" />
<Button
android:id="#+id/fantab"
android:layout_width="66dp"
android:layout_height="66dp"
android:layout_alignParentTop="true"
android:layout_marginRight="95dp"
android:layout_toLeftOf="#+id/navi"
android:background="#drawable/fan_tab"
android:clickable="true" />
</RelativeLayout>
and here is my logcat
http://i.stack.imgur.com/UDUQC.png
any ideas?
You have that findViewById outside the onCreate method and it's not located inside any method.
Also you are defining Button blade twice.
So once you have defined button blade, then while referencing to it, you should not define it again.
So your your line should be -
blade = (Button)findViewById(R.id.fantab);
The error is about a null pointer on this line:
Button blade = (Button)findViewById(R.id.fantab);
Since you have the button in your xml, most likely you are not pointing to the right layout file:
setContentView(R.layout.main);
It means the layout file must be main.xml or you change the code to point to the right layout.
setContentView(R.layout.Layout);//from your post "here is my Layout.xml"
cause by: java.lang.NullPointerException
at android.app...FindviewbyID
at android.app.....main<init>(main.java:48);
the problem is there are object that findviewbyID by wrong reference. check carefull in line 48.
so apparently the problem was that the findViewById was after the bracket of the OnCreate function. Thank you for your answers.

The emulator isn't showing the XML contents

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

Starting activities with intents in Android - why does this work?

Previously I've been looking at existing code to display a map in a tab, and this time I've tried to write it myself to see if I understood it.
It runs fine without errors (screenshot), but I don't completely understand why, and whether I'm doing this in the best way.
The part that I don't quite understand is in the manifest.
I would have thought that android:label should be maptabview_name, but that gives me an error saying no matching resource was found.
Why does it run when using app_name for that activity?
Why can't it find the maptabview_name resource?
Also, in MapTab2, is this the best way to start an intent?
What exactly is this telling the system? "I intend to start an activity from this class"?
Here's my code
(It's based on this):
MapTab2.java
package com.test.maptab2;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class MapTab2 extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = new Intent(this,MapTabView.class);
TabHost.TabSpec spec;
spec = getTabHost().newTabSpec("tab1");
spec.setContent(i);
spec.setIndicator("Map");
getTabHost().addTab(spec);
spec = getTabHost().newTabSpec("tab2");
spec.setContent(R.id.detailstub);
spec.setIndicator("Detail");
getTabHost().addTab(spec);
getTabHost().setCurrentTab(0);
}
}
MapTabView.java
package com.test.maptab2;
import android.os.Bundle;
import com.google.android.maps.MapActivity;
public class MapTabView extends MapActivity {
#Override
public void onCreate (Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.maptabview);
}
#Override
public boolean isRouteDisplayed(){
return false;
}
}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- Tab-switch panel -->
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<!-- Tab contents -->
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Map here -->
<RelativeLayout android:id="#+id/mapstub"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<!-- Other stuff -->
<TextView android:id="#+id/detailstub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="detail"/>
</FrameLayout>
</LinearLayout>
</TabHost>
maptabview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/maptablayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="0HRMcD5o6WrBVhmwbWpeyeavZ67PXWOvJeeCx2g"/>
</RelativeLayout>
MapTab2.Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.maptab2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<uses-library android:name="com.google.android.maps" />
<!-- Main -->
<activity android:name=".MapTab2"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Map -->
<activity android:name=".MapTabView"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.EMBED" />
</intent-filter>
</activity>
</application>
</manifest>
Q1: maptabview_name has to be in a string resource file, usually at res/values/string.xml, in case you have it there and still it's not in the resources generated file (gen/package/R.java), try removing it and let eclipse generate it again.
Q2: AFAIK, TabHost.TabSpec.setContent(Intent intent) it's like starting the Activity within the specified tab. I think that's the way do do it when you deal with tabs. Since Intents are used to do a lot of things, this is a basic usage, in this case just to specify the class.

SurfaceView: ClassCastException, unable to start activity

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>

A new button to Android screen

How can I display a button on screen? I have defined it as
final Button nappi = (Button) findViewById(R.id.soita);
and
<Button
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="#+string/Soita"
android:id="#+id/soita"
/>
You need to create the layout of the view in which you want the button to appear. Even something like
Let's say you're storing the definition in a file called main.xml in your res/layout directory.
<?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"
>
<Button android:text="Button01" android:id="#+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
Then within your activity, you need to do the following:
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Replace this with the name of your xml file dictating layout
setContentView(R.layout.main);
}
}
See the Hello Views tutorial; very useful. http://developer.android.com/guide/tutorials/views/index.html
If you're asking something different, like programmatically adding a button to an existing view, I'm not sure about that. In that case you might want to have the button start off as hidden and then reveal it when you need it.
I don't see why this displays nothing. I tried to make my own simple user interface.
main:
package com.xyz;
import android.app.Activity;
import android.os.Bundle;
public class NewHomeScreen extends Activity {
/** Called when the activity is first created. */
#Override public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.main);
}
}
XML:
<?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="fill_parent"
android:text="#string/hello"
>
</TextView>
<Button android:text="Soita"
android:id="#+id/soita"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</Button>
</LinearLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xyz"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name="NewHomeScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
Your textview gets all screen place...
try this
XML:
<TextView
android:text="#string/hello"
android:layout_width="match_parent" android:layout_height="291dp">
</TextView>

Categories

Resources