how to show to battery status on text view? - android

Actually i am trying to make an android app to display a text when the battery becomes 50% ,m using text view to display the msg,but while loading the app it gets unfortunately stops.
here is my code:
Java file.
package com.example.abhi.batteryalarm;
import android.content.Intent;
import android.os.BatteryManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class Battery_Alarm extends ActionBarActivity {
Intent batteryStatus;
TextView txtView;
String hello;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_battery__alarm);
txtView=(TextView)findViewById(R.id.txtView);
hello="This is my first project";
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
if (level==50)
{
txtView.setText(hello);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_battery__alarm, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity file:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".Battery_Alarm">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:textSize="62sp"
android:id="#+id/txtView"
android:layout_centerHorizontal="true"
android:layout_marginBottom="125dp" />
</RelativeLayout>
What things i need to do?

Add this,
batteryStatus = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
before,
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
and it will work.

This is the line which is causing the error :
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
It's because the batteryStatus intent is never initialized and is hence null.
Check out this answer about fetching the battery level : https://stackoverflow.com/a/4047790/1239966

You need to register batteryStatus.
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);

Related

I want to publish geosever layer in android app, using arcgis SDK, i have done code but it is not working, please tell me what i am doing wrong?

I am doing Android programming for the first time, I want to display a GeoServer layer using the ArcGis SDK map layer. I have done the code but it is not working, please tell me what i am doing wrong.
activity_main.xml
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.subrata.mymap.MainActivity">
<com.esri.android.map.MapView
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
mapoptions.MapType="Aerial"
mapoptions.center="28, 77"
mapoptions.ZoomLevel="10">
</com.esri.android.map.MapView>
</RelativeLayout>
mainactivity.java
package com.example.subrata.mymap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.esri.android.map.MapView;
import com.esri.android.map.ogc.WMSLayer;
public class MainActivity extends AppCompatActivity {
MapView mMapView;
WMSLayer wmsLayer;
String wmsURL;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMapView = (MapView)findViewById(R.id.map);
wmsURL = "http://localhost:8081/geoserver/NFR-SR/wms";
wmsLayer = new WMSLayer(wmsURL);
wmsLayer.setImageFormat("application/openlayers");
// available layers
String[] visibleLayers = {"NFR-SR:Ridgeline_SR"};
wmsLayer.setVisibleLayer(visibleLayers);
wmsLayer.setOpacity(0.5f);
mMapView.addLayer(wmsLayer);
mMapView.setEsriLogoVisible(false);
mMapView.enableWrapAround(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Can't really tell what you mean by not working without any log or explanation.
Here are some possible reason:
cannot reach server at url http://localhost:8081/geoserver/NFR-SR/wms
layer name NFR-SR:Ridgeline_SR not found
map service doesn't support application/openlayers
It could even be it is working but you are viewing the map extent which does not contain any data.

Error inflating class fragment in Android popular movies app

After i made some changes in an AsynTask i get this error when running the app:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.popularmovies/com.example.android.popularmovies.MainActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
This is the activity_main.xml file.
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment"
android:name="com.example.android.popularmovies.MainActivityFragment"
tools:layout="#layout/fragment_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
And this is the MainActivity.java file:
package com.example.android.popularmovies;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Thanks for any help you can provide! ;)
I am not sure try the following:
Add
tools:context= ".MainActivity"
or Can you put <fragment> inside a <FrameLayout>.
Clean the code once.

Could-not create the view: org.eclipse.pde.runtime.LogView

I am new to Android and building a small test program and the program is not running and giving me Could not create the view: org.eclipse.pde.runtime.LogView error
here is the code from my android_main.xml file
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.myapplication.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
Here is the java code from MainActivity.Java
package com.example.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I apologize that I am posting this as an answer instead of a comment, but commenting requires 50 reputation and I don't have that yet. Can you post your java code too? The problem is likely in there, it may be the method by which you are loading the view.

How to solve inconvertable types cannot cast "Android.support.v4.app.fragment" to "packagename"

I am creating an android application that consists of navigation drawer in android studio.
I am getting an error called inconvertable types cannot cast "How to solve inconvertable types cannot cast "Android.support.v4.app.fragment" to "packagename"" please helpme howto solve this.
This is my activity_main.java
package sample.lakshman.com.sampleltester;
import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.widget.Toolbar;
import sample.lakshman.com.sampleltester.Fragment_navigation;
public class MainActivity extends ActionBarActivity {
public Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
Fragment_navigation drawer_navigation = (Fragment_navigation)getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawer_navigation.setUp((DrawerLayout)findViewById(R.id.drawer_layout),toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if(id==R.id.navigation_item)
{
Intent sub = new Intent(MainActivity.this,Subactivity.class);
startActivity(sub);
}
return super.onOptionsItemSelected(item);
}
}
This is my main_activity.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar" />
</RelativeLayout>
<fragment
android:id="#+id/fragment_navigation_drawer"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_fragment_navigation"
android:name="sample.lakshman.com.sampleltester.Fragment_navigation"
tools:layout="#layout/fragment_fragment_navigation" />
</android.support.v4.widget.DrawerLayout>
Just go to your Fragment_navigation class and
replace
import android.app.Fragment;
with
import android.support.v4.app.Fragment;

Get TimePicker Current Time on Button Click

Friends
I am new on Android App Developing. I have nearly completed my app.
In the last stage, I'm facing difficulties and trying for the last three days to solve it but could not.
I need to get the current hour and minutes from a time picker (without Dialog) when the user clicks a button. I want to save this data to a database on this button click.
Would you anybody please help me providing just an XML and JAVA file as below.
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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.timepicker.MainActivity" >
<TimePicker
android:id="#+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="21dp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/timePicker1"
android:layout_centerHorizontal="true"
android:layout_marginTop="77dp"
android:text="Button" />
</RelativeLayout>
Java:
package com.example.timepicker;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Using Calendar instance you can get current hour and minutes-
Calendar c;
private int mhour;
private int mminute;
initialize -
c = Calendar.getInstance();
mhour = c.get(Calendar.HOUR);
mminute = c.get(Calendar.MINUTE);
// you will get current hour and minutes of device time
Also refer this Calendar

Categories

Resources