为什么MainActivity无法解决或不是字段?(Why MainActivity cannot be resolved or is not a field?)
我有一个小型项目,我需要使用Android设备的电池。 起初,我希望能够打印电池,所以我使用了本教程 。
然后,我在Eclipse中创建了一个名为“Testing”的新Android项目,并在MainActivity.java中添加了以下代码:
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; } }
实际上,这应该只是在屏幕上打印电池电量的情况下执行简单的活动。 但这并不是因为一个错误。 “MainActivity无法解析或不是字段”(请参阅注释行)。 你有什么想法,为什么? 我想让这段代码有效,所以我可以继续。
谢谢。
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 have 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; } }
Actually this should just run a simple activity with the Battery level printed on the screen. But it does not because of one error. "MainActivity cannot be resolved or is not a field" (see the commented line). Do you have any idea why ? I would like to have this code working so I can go on then.
Thank you.
原文:https://stackoverflow.com/questions/21852485
最满意答案
你可以试试这个
class ViewController: UIViewController { let screenHeight = UIScreen.main.bounds.height let screenWidth = UIScreen.main.bounds.width lazy var sideBarUIView: UIView! = { let sideBarUIView = UIView() sideBarUIView.backgroundColor = UIColor.red sideBarUIView.translatesAutoresizingMaskIntoConstraints = false return sideBarUIView }() override func viewDidLoad() { super.viewDidLoad() view.addSubview(sideBarUIView) sideBarUIView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true sideBarUIView.heightAnchor.constraint(equalToConstant: screenHeight).isActive = true sideBarUIView.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.5).isActive = true sideBarUIView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true } }
You can try this
class ViewController: UIViewController { let screenHeight = UIScreen.main.bounds.height let screenWidth = UIScreen.main.bounds.width lazy var sideBarUIView: UIView! = { let sideBarUIView = UIView() sideBarUIView.backgroundColor = UIColor.red sideBarUIView.translatesAutoresizingMaskIntoConstraints = false return sideBarUIView }() override func viewDidLoad() { super.viewDidLoad() view.addSubview(sideBarUIView) sideBarUIView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true sideBarUIView.heightAnchor.constraint(equalToConstant: screenHeight).isActive = true sideBarUIView.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.5).isActive = true sideBarUIView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true } }
相关问答
更多-
所以,我决定拒绝ImageView,只是把我的图像作为背景(下面有空白区域)也许我的代码会帮助某人(但是,嘿,更好找到更有用的解决方案) public static void setDrawerBackground(MainActivity activity, Bitmap if (bitmap==null) return; Point p = new Point(); activity.getWindowManager().getD ...
-
好吧,错误是由于状态栏的高度为20px,而iphone屏幕的高度为480px。 因此,如果您创建一个高度为480px且状态栏位于其上方的应用程序,则应该发生这种混乱。 所以我建议你要么让你的视图高460像素,要么隐藏状态栏,如果20px对你很重要。 如果要隐藏状态栏,则在info.plist中添加另一个名为“状态栏最初隐藏”的变量,然后单击列表末尾的复选框。 well the error is caused due to the fact that the status bar is 20px in hei ...
-
你可以试试这个 class ViewController: UIViewController { let screenHeight = UIScreen.main.bounds.height let screenWidth = UIScreen.main.bounds.width lazy var sideBarUIView: UIView! = { let sideBarUIView = UIView() sideBarUIView.backgr ...
-
最后我自己找到了答案。 我们需要在- (void) viewDidLayoutSubviews方法中偏移框架,如下所示, - (void) viewDidLayoutSubviews { [super viewDidLayoutSubviews]; if([self respondsToSelector:@selector(edgesForExtendedLayout)]) { CGRect frame = self.view.frame; frame ...
-
使用ApplicationMessageFolderRegistry和ApplicationMessageFolder类以及ApplicationMessage和ApplicationMessageFolderListener接口。 Use the ApplicationMessageFolderRegistry and ApplicationMessageFolder classes, and the ApplicationMessage and ApplicationMessageFolderListe ...
-
更新: 在聊天中解决,只需要将不透明视图放在scrollview前面,而不是将其作为scrollview的子视图。 您可以为滚动视图关闭弹跳,但这看起来并不顺畅。 Update: Solved in chat, just needed to put the opaque view in front of the scrollview and not have it as a subview of the scrollview. You could turn bounces off for the scrol ...
-
如何在iPad应用程序隐藏状态栏的区域中获取触摸事件?(How Can I Get touch events in an iPad App's hidden status bar's area?)[2022-01-31]
如果你非常绝望,你可以在(看不见的)状态栏顶部拥有自己的窗口,并在那里处理事件。 设置它的windowLevel足够高,你很高兴。 但是,这对您来说可能不是一个好的解决方案,因为无法将触摸从一个窗口转移到另一个窗口。 您最终可能需要自己进行大量的触控操作。 If you're desperate enough, you could have your own window on top of the (invisible) status bar and process events in there. Se ... -
从iOS 7开始,状态栏不再自动影响应用的大小/布局。 你的红色UIView仍然是400px高; 40个像素被隐藏在通话中状态栏后面。 如果您希望UI元素遵守状态栏高度/状态的更改,则必须以编程方式执行此操作。 您可以在应用委托中实现application:willChangeStatusBarFrame:和/或application:didChangeStatusBarFrame来解决此问题: - (void)application:(UIApplication *)application willCha ...
-
状态栏下方的UIView(UIView underneath Status Bar)[2023-03-02]
你可以通过在viewDidLoad方法甚至viewWillAppear设置它来解决这个问题: self.view.frame = [[UIScreen mainScreen] applicationFrame]; 另外,我猜你最初是在隐藏你的状态栏? 或者你有它总是显示? You can fix this by setting this in your viewDidLoad method or even the viewWillAppear: self.view.frame = [[UIScreen m ... -
实际上,我完全错了,请查看此链接: http : //initwithfunk.com/blog/2014/02/07/how-to-build-the-facebook-paper-status-bar-hover/ Actually, I was completely wrong, check this link: http://initwithfunk.com/blog/2014/02/07/how-to-build-the-facebook-paper-status-bar-hover/