setText不显示变量

时间:2015-06-30 16:30:05

标签: java android

当我运行应用程序时,TextView中没有显示任何内容。我觉得它与onReceive函数有关,因为当我将它移出它显示的函数时。如果是这种情况,我如何获得函数外的变量值?

我刚刚将我的代码更新为新的获取方式,但现在我收到此错误。有任何想法吗?

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null
     

对象引用               at>   com.example.android.login.MainActivity $ 1.onReceive(MainActivity.java:42)

   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);
    }

}

1 个答案:

答案 0 :(得分:0)

创建接收器后,您需要在活动中添加registerReceiver()。现在,您永远不会执行onReceive()代码。