如何在片段___android中更新textview?

时间:2014-08-20 22:45:25

标签: android android-fragments textview broadcastreceiver

我正在开发一款能够显示电池温度和其他电池信息的应用。 因为我已经实现了带片段的滑动标签。 但是当我尝试使用电池温度更新片段中的textview时,应用程序强制关闭。代码不显示任何错误。它是运行时错误。 片段xml文件只有relativelayout和textview。 如果我不尝试更新温度,swipetabs工作得很好。 电池温度在没有选项卡的布局中运行时显示良好。 只有2个标签,我使用braodcast_receiver获取电池信息。 请为此提供一些解决方案

温度片段代码

package com.example.batterytemppro;


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.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class temprature_fragment extends Fragment{

TextView tvtemp;
BatteryManager batteryman;
 Context context;;
 double i,j;
 //Intent intent;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    View rootview= inflater.inflate(R.layout.temprature_xml, container, false);
    tvtemp=(TextView)rootview.findViewById(R.id.textViewtemp);
    //tvtemp.setText("this is not working");

    batteryknow bk=new batteryknow();
    context.getApplicationContext();
    context.registerReceiver(bk, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));


    return rootview;



    }


public class batteryknow extends BroadcastReceiver {


    BatteryManager bm;
    TextView tvtemp;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub


         i =intent.getIntExtra(bm.EXTRA_TEMPERATURE, 0);
         j=i/10;
        tvtemp.setText(""+j);

    }

}   

}

mainactivity.class的代码

package com.example.batterytemppro;   


//import com.example.batterytemppro.temprature_fragment.batteryknow;

import android.os.BatteryManager;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;

import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements TabListener, 
OnPageChangeListener {

ViewPager viewpager;
ActionBar actionbar;
String [] tabs={"Temperature","Information"};
temprature_fragment tempt;
double i,j;
TextView tvtemp;
Intent intent;
BatteryManager bm;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    viewpager=(ViewPager)findViewById(R.id.pager);
    actionbar=getActionBar();
    actionbar.setHomeButtonEnabled(false);
    actionbar.setNavigationMode(actionbar.NAVIGATION_MODE_TABS);


    viewpager.setAdapter(new TabpageAdapter(getSupportFragmentManager()));
    viewpager.setOnPageChangeListener(this);



    for(String tab_host:tabs)
    {
        actionbar.addTab(actionbar.newTab().setText(tab_host).setTabListener(this));

    }   

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction arg1) {
    // TODO Auto-generated method stub
    viewpager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub

}

@Override
public void onPageScrollStateChanged(int arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
    // TODO Auto-generated method stub

}

@Override
public void onPageSelected(int position) {
    // TODO Auto-generated method stub
    actionbar.setSelectedNavigationItem(position);

  }

}

pageadpter类的代码

package com.example.batterytemppro;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabpageAdapter extends FragmentPagerAdapter{

    public TabpageAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

@Override
public Fragment getItem(int position) {
    // TODO Auto-generated method stub

   switch (position) {

   case 0:

       return new temprature_fragment();

   case 1:

       return new information_fragment();


}           

    return null;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 2;
}   

}

1 个答案:

答案 0 :(得分:0)

您实际上是在尝试从运行与UI线程不同的线程的BroadcastReceiver更改textview。 Android不允许您从UI线程外部对UI进行更改。将您BroadcastReceiver更改为:

public class batteryknow extends BroadcastReceiver {


    BatteryManager bm;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub


         i =intent.getIntExtra(bm.EXTRA_TEMPERATURE, 0);
         j=i/10;

         runOnUiThread(new Runnable() 
         {
             public void run() 
             {
                 tvtemp.setText(""+j);
             }
         });                
     }
}   

你需要改变的最后一件事是tvtemp(TextView)为空。你必须在片段类

中将它声明为final