Button ClickListener crashes app - android

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.

Related

Android Keyboard Input

I started programming an app and on startup there is always an text input with a blinking cursor at the bottom. I have no EditText defined in the xml file and wont need any text input.
I have no idea why it shows that part. Can anyone help me getting rid of that?
EDIT:
I added the Manifest File.
Here the current *.xml file
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context="de.test.ble_benchmark.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/nodevice"
android:id="#+id/device"
android:textSize="20sp"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="190dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="336dp"
android:layout_alignTop="#+id/console"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/console"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="144dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/scan"
android:id="#+id/scan"
android:textSize="25sp"
android:textStyle="bold"
android:onClick="onClick"
android:layout_gravity="center"
android:layout_marginLeft="134dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.tuhh.et5.tills.ble_benchmark">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
>
<activity android:windowSoftInputMode="stateHidden" android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".BlunoService"/> >
</application>
</manifest>
EDIT:
Ok, there seems to be more to it. I'm not sure, but it seems related..
I try to change the visibility of those TextViews and get a nullpointer exception. Something is messed up ...
I cant figure out. Here is the method that generates the exception:
public void updateUi(){
Button scan = (Button)findViewById(R.id.scan);
ProgressBar search = (ProgressBar)findViewById(R.id.progressBar);
TextView output = (TextView)findViewById(R.id.output);
TextView deviceName = (TextView)findViewById(R.id.device);
switch (layoutState){
case 0:
// close
super.onBackPressed();
cleanup_bluno();
d("State 0");
break;
case 1:
// Start
d("State 1");
search.setVisibility(View.GONE);
deviceName.setVisibility(View.GONE);
scan.setVisibility(View.VISIBLE);
output.setVisibility(View.VISIBLE);
break;
case 2:
// Searching
d("State 2");
search.setVisibility(View.VISIBLE);
deviceName.setVisibility(View.GONE);
scan.setVisibility(View.GONE);
output.setVisibility(View.GONE);
break;
case 3:
// Device Found
d("State 3");
search.setVisibility(View.GONE);
deviceName.setVisibility(View.VISIBLE);
scan.setVisibility(View.GONE);
output.setVisibility(View.VISIBLE);
break;
default:
break;
}
}
It starts into state one and crashes when setting the Visibilities.
Add this line of code in your activity onCreate() method. I think the following may work
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Ok, i'm a little embarrassed... Someone told me to run "invalidate Caches / Restart". Now the problem dissapeared...
Sorry for bothering and thanks for looking into it :-)

Error Binary XML file line #2: Error inflating class on android programming

I have an android program with 2 layouts. The first layout is Login. When the login is started first, I have no problem. When I try to change in AndroidManifest from login to splash layout, I got an error. The error is like the title of this posts.
In this android manifest, I change my code from :
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
>
<activity
android:name=".Login"
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>
to :
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
>
<activity
android:name=".Splash"
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>
Here is the layout for login:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".Login">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:textSize="45dp"
android:gravity="center"
android:id="#+id/tvDisplay"
/>
<Button
android:layout_width="250dp"
android:layout_height="100dp"
android:text="Add One"
android:gravity="center"
android:layout_gravity="center"
android:id="#+id/bAdd"
android:textSize="20dp"/>
<Button
android:layout_width="250dp"
android:layout_height="100dp"
android:text="Substract One"
android:gravity="center"
android:layout_gravity="center"
android:id="#+id/bSub"
android:textSize="20dp"/>
And here the layout of splash:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="#drawable/sertifikat"
tools:context=".Splash"></LinearLayout>
And structure of my code :
Log in logcat:
Here splash.java
How can I fix my code?
Hey You missed some small things
Here is a small example from your code
on Github that i wrote
This is running example of your code
Other than the ending tags
The <intent-filter>
should only be one
most probably the problem lies in your #drawable
as well as look at these tutorials they are great
http://www.androidhive.info/2013/07/how-to-implement-android-splash-screen-2/
Obviously,you have lost a character '/' in the splash layout at the end.
You are missing the closing tag of your LinearLayout in splash.xml. Add </LinearLayout> to the end of the file. You also might want to consider using an ImageView with android:scaleType="centerCrop" instead of using a LinearLayout with a background. Using android:background will stretch to fit the size of the screen, which can give a different aspect ratio on different devices, whereas an ImageView will scale it to maintain the aspect ratio (depending on the scaleType).
Also, a LinearLayout is not appropriate for a single image, since it is meant to be a container for other views.
For better performance I recommend you to follow this :
http://www.cutehacks.com/blog/2014/01/13/splash-screens
If you use an high end device you won't see a big difference, but for common devices you will see an huge improvement

error in running application on android emulator

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>

this program failed to install on emulator or mobile phone. why?

I write the program and i try to run the program but this program have 2warning :( and when i try to run with emulator, the program is failed. when i run with mobile phone, mobile was hang, even with disconnected mobile with pc, mobile is still hanging:(
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="30dip"
android:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center">
<TextView
android:text="#string/main_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginBottom="25dip"
android:textSize="24.5sp"/>
<Button
android:id="#+id/continue_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/continue_label"/>
<Button
android:id="#+id/new_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/new_game_label"/>
<Button
android:id="#+id/about_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/about_label" />
<Button
android:id="#+id/exit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/exit_label"/>
</LinearLayout>
strings.xml
<resources>
<string name="app_name">Sudoku</string>
<string name="menu_settings">Menue</string>
<string name="title_activity_main">MianActivity</string>
<string name="main_title">Android Sudoku</string>
<string name="continue_label">Continue</string>
<string name="new_game_label">New Game</string>
<string name="about_label">About</string>
<string name="exit_label">Exit</string>
</resources>
annd AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.sudoku"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="15" />
<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" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
and warning errors are:
of line 9 --> This LinearLayout layout or its LinearLayout parent is useless; transfer the background attribute to the other view
of line 13 --> Unexpected text found in layout file: """
You dont need a nested LinearLayout Here. Because there are no other children to the first LinearLayout. Remove the first LinearLayout. It is useless.

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

Categories

Resources