Java setText不显示变量
当我运行应用程序时,文本视图中没有显示任何内容。我有一种感觉,它与onReceive函数有关,因为当我将它移出显示的函数时。如果是这种情况,我如何获得函数外部变量的值 我刚刚用新的方式更新了我的代码,但是现在我发现了这个错误。有什么想法吗 对象引用 在> com.example.android.login.MainActivity$1.onReceive(MainActivity.java:42)Java setText不显示变量,java,android,Java,Android,当我运行应用程序时,文本视图中没有显示任何内容。我有一种感觉,它与onReceive函数有关,因为当我将它移出显示的函数时。如果是这种情况,我如何获得函数外部变量的值 我刚刚用新的方式更新了我的代码,但是现在我发现了这个错误。有什么想法吗 对象引用 在> com.example.android.login.MainActivity$1.onReceive(MainActivity.java:42) 创建接收器后,您需要向活动中添加一个registerReceiver()。现在,您将永远不会让您的
创建接收器后,您需要向活动中添加一个
registerReceiver()
。现在,您将永远不会让您的onReceive()
代码执行。您显示的代码是否编译?我认为之所以没有编译,是因为findViewbyId()
正在搜索活动以获取视图,所以在广播接收器外部它可以正常工作,但在内部,我认为它无法引用视图。如果将findViewbyId()
行移到广播接收器外部,但将setText()
行留在内部,它可能会工作。这是否在活动内部?你可以做ActivityName.this.findViewById()
来做你想做的事。你的接收器真的发射过吗?在创建它之后,您似乎并没有对它做任何处理。@更不用说,这是因为他是从一个匿名的内部类中访问它的。这是必需的。
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null
package com.example.android.login;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.BatteryManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.parse.LogOutCallback;
import com.parse.Parse;
import com.parse.ParseObject;
import com.parse.ParseUser;
import com.parse.ParseException;
public class MainActivity extends ActionBarActivity {
private TextView batteryPercent;
public void getBatteryPercentage() {
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(this);
int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int level = -1;
if (currentLevel >= 0 && scale > 0) {
level = (currentLevel * 100) / scale;
}
batteryPercent.setText(level);
}
};
IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}
private Toolbar toolbar;
public Button logoutButton;
public int level;
@Override
public void onCreate(Bundle savedInstanceState) {
batteryPercent = (TextView) this.findViewById(R.id.percent);
getBatteryPercentage();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_appbar);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawerLayout), toolbar);
logoutButton = (Button) findViewById(R.id.logoutButton);
logoutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Set up a progress dialog
final ProgressDialog logout = new ProgressDialog(MainActivity.this);
logout.setTitle("Please wait.");
logout.setMessage("Logging out. Please wait.");
logout.show();
ParseUser.logOutInBackground(new LogOutCallback() {
public void done(ParseException e) {
logout.dismiss();
if (e == null) {
Intent logoutDone = new Intent(MainActivity.this, DispatchActivity.class);
startActivity(logoutDone);
} else {
Toast.makeText(MainActivity.this, "Logout Unsuccessful", Toast.LENGTH_LONG).show();
}
}
});
}
});
}
@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);
}
}