Why can't MainActivity be allowed or not a field?

I have a small project where I need to play with the Battery of an Android device. At first, I wanted to be able to print the Battery, so I used this tutorial .

Then I created a new Android project called "Testing" in Eclipse, and in MainActivity.java I put this code:

package com.example.testing;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
import android.R.*;



public class MainActivity extends Activity {  
     TextView mTextView;  
     @Override  
     protected void onCreate(Bundle savedInstanceState) {  
          super.onCreate(savedInstanceState);  
          setContentView(R.layout.MainActivity); //This is the MainActivity that contains the error
          mTextView = (TextView) findViewById(R.id.batterTest);  
          findViewById(R.id.getBattery).setOnClickListener(new OnClickListener() {  
               @Override  
               public void onClick(View v) {  
                    mTextView.setText(String.valueOf(batteryLevel));  
               }  
          });  
          new Thread(new ThreadM()).start();  
     }  
     @Override  
     protected void onDestroy() {  
          super.onDestroy();  
          unregisterReceiver(mArrow);  
     }  
     BatteryReceiver mArrow;  
     private class ThreadM implements Runnable {  
          @Override  
          public void run() {  
               mArrow = new BatteryReceiver();  
               IntentFilter mIntentFilter = new IntentFilter();  
               mIntentFilter.addAction(Intent.ACTION_BATTERY_LOW);  
               mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);  
               mIntentFilter.addAction(Intent.ACTION_BATTERY_OKAY);  
               Intent batteryIntent = registerReceiver(mArrow, mIntentFilter);  
               batteryLevel = getBatteryLevel(batteryIntent);  
               Log.e("Battery Level", String.valueOf(batteryLevel));  
          }  
     }  
     float batteryLevel;  
     private class BatteryReceiver extends BroadcastReceiver {  
          @Override  
          public void onReceive(Context arg0, Intent arg1) {  
               if (arg1.getAction().equalsIgnoreCase(Intent.ACTION_BATTERY_LOW)  || arg1.getAction().equalsIgnoreCase(Intent.ACTION_BATTERY_CHANGED) || arg1.getAction().equalsIgnoreCase(Intent.ACTION_BATTERY_OKAY)) {  
                    int level = arg1.getIntExtra("level", 0);  
                    Toast.makeText(MainActivity.this,"Current Battery " + level + " %", Toast.LENGTH_LONG).show();  
                    mTextView.setText(String.valueOf("Battery Level Change Detect through Receiver = " + level));  
               }  
          }
     }  
     public float getBatteryLevel(Intent batteryIntent) {  
          int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);  
          int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);  
          if (level == -1 || scale == -1) {  
               return 50.0f;  
          }  
          return ((float) level / (float) scale) * 100.0f;  
     }  
}

In fact, it should just start a simple activity with the battery level printed on the screen. But this is not due to one mistake. "MainActivity cannot be enabled or is not a field" (see Commented line). Do you know, why? I would like this code to work, so I can continue.

Thank.

+3
source share
1

 setContentView(R.layout.MainActivity); 

 setContentView(R.layout.activity_main); 

, activity_main.xml, MainActivity, Activity

 import android.R.*;

 import com.example.testing.R;
+9

All Articles