На самом деле, я пытаюсь сделать приложение для Android, чтобы отобразить текст, когда батарея станет 50%, т с помощью текстового вида, чтобы отобразить сообщение, но при загрузке приложения он, к сожалению, останавливается.Как показать состояние батареи в текстовом режиме?
вот мой код:
Java файл.
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);
}
}
активность файла:
<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>
Какие вещи я должен сделать?
Используя широковещательный приемник, вы получаете текущий статус. –
Исключение. Вставьте журналы – Psypher
, где вы получили этот код? post logcat всегда, когда у вас ошибка, i thuink 'batteryStatus' не назначается, поэтому он имеет значение null, это проблема, – Yazan