zoukankan      html  css  js  c++  java
  • 动态修改TextView 的问题

    今天遇到一个问题,没办法动态修改TextView 的text 会有NullPointerException, 似乎只能在onCreate里面setConentView 之后才能修改,成员函数什么的都不行。最后无奈我只有把整个图都动态编写了。留着我以后慢慢研究

     1 package com.example.contextawaredemo;
     2 
     3 
     4 import android.app.Activity;
     5 import android.os.Bundle;
     6 import android.view.View;
     7 import android.view.ViewGroup;
     8 import android.widget.CompoundButton;
     9 import android.widget.LinearLayout;
    10 import android.widget.TextView;
    11 import android.widget.Toast;
    12 import android.widget.ToggleButton;
    13 
    14 
    15 public class SwitchTV extends Activity{
    16     static TextView tv;
    17     static ToggleButton tb;
    18     LinearLayout linlayout;
    19     SimplePost post=new SimplePost();
    20     String success="0";
    21     
    22     @Override
    23     protected void onCreate(Bundle savedInstanceState){
    24         super.onCreate(savedInstanceState);
    25 //        setContentView(R.layout.switch_tv);
    26 //        tv = (TextView) findViewById(R.id.TVswitch);
    27         tv=new TextView(this);
    28         tv.setText("the tv status");
    29         
    30         tb=new ToggleButton(this);
    31         tb.setVisibility(View.GONE);
    32         tb.setText("ON");
    33         linlayout=new LinearLayout(this);
    34         linlayout.setOrientation(LinearLayout.VERTICAL);
    35         LinearLayout.LayoutParams Params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
    36         linlayout.addView(tv,Params);
    37         linlayout.addView(tb,Params);
    38         setContentView(linlayout);
    39 //        tb=(ToggleButton) findViewById(R.id.TBswitch);
    40         
    41         NetworkReceiver networkReceiver=new NetworkReceiver();
    42         System.out.println("new Network receiver created");
    43         tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
    44 
    45         {
    46 
    47             @Override
    48             public void onCheckedChanged(CompoundButton buttonView,
    49                     boolean isChecked) {
    50                 if(post.sendMessage("powerOn").equals(success)){
    51                     Toast.makeText(getBaseContext(), "tv is on", Toast.LENGTH_LONG).show();
    52                 }
    53                 else{
    54                     Toast.makeText(getBaseContext(), "try again", Toast.LENGTH_LONG).show();
    55                 }
    56                 
    57                 
    58             }});
    59         
    60     }
    61     @Override
    62     public void onDestroy() {
    63         super.onDestroy();
    64         
    65     }
    66     
    67     public void statusOn(){
    68         //tv = new TextView(this);
    69         
    70         //tv = (TextView) findViewById(R.id.TVswitch);
    71         tv.setText("the tv is in status on");
    72         
    73     }
    74     
    75     public void statusOff(){
    76         
    77         
    78         tv.setText("the tv is in status off, click the toggle to turn on it");
    79         tb.setVisibility(View.VISIBLE);
    80     }
    81 
    82 }
  • 相关阅读:
    协同过滤
    深度学习中 epoch,[batch size], iterations概念解释
    如何查看Python内置模块的实现代码
    机器学习/数据挖掘/算法岗位
    算法工程师B
    算法工程师A
    web性能测试基本性能指标
    Loadrunner11不能调用IE8解决方法大全
    抓取Android应用的log
    关于字符latin capital letter sharp s "ß"( U+1E9E)显示的问题
  • 原文地址:https://www.cnblogs.com/lauraxia/p/2969707.html
Copyright © 2011-2022 走看看