Android textview的文本错误

时间:2014-03-29 15:09:39

标签: android nullpointerexception textview

有2节课。

头等舱:

public class GecmisGoruntule extends Activity 
{
    public static TextView t1;
    TextView t2;
    TextView t3;
    TextView t4;
    TextView t5;

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gecmisgoruntule);
        t1=(TextView)findViewById(R.id.textView1);
        t2=(TextView)findViewById(R.id.textView2);
        t3=(TextView)findViewById(R.id.textView3);
        t4=(TextView)findViewById(R.id.textView4);
        t5=(TextView)findViewById(R.id.textView5);
    }
}

我的第二堂课:

public class Tab3 extends Activity 
{
    yuksekPuanDb v;
    Intent intent;
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab3);
        v=new yuksekPuanDb(this);


          intent = new Intent().setClass(this, GecmisGoruntule.class);
        liste.setOnItemClickListener(new OnItemClickListener() 
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) 
            {

                        GecmisGoruntule.t1.setText("deneme");
                        startActivity(intent);
            }
        });
    }
}

头等舱有5个文字视图。我在第二堂课中从db获取数据,我想更改我的文本浏览文本.Tab3我的第二堂课。

但我得到错误:

03-29 17:04:51.570: E/AndroidRuntime(24835): FATAL EXCEPTION: main    
03-29 17:04:51.570: E/AndroidRuntime(24835):    java.lang.NullPointerException 
03-29 17:04:51.570: E/AndroidRuntime(24835): at com.its.android.Tab3$1.onItemClick(Tab3.java:56)

1 个答案:

答案 0 :(得分:1)

试试这个..

您无法设置其他活动textview。将值传递给该活动获取并打印它

    liste.setOnItemClickListener(new OnItemClickListener() 
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) 
        {

                    intent.putExtra("name","deneme");
                    startActivity(intent);
        }
    });

public class GecmisGoruntule extends Activity 
{
    public static TextView t1;
    TextView t2;
    TextView t3;
    TextView t4;
    TextView t5;

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gecmisgoruntule);
        t1=(TextView)findViewById(R.id.textView1);
        t2=(TextView)findViewById(R.id.textView2);
        t3=(TextView)findViewById(R.id.textView3);
        t4=(TextView)findViewById(R.id.textView4);
        t5=(TextView)findViewById(R.id.textView5);

        if(getIntent().hasExtra("name")){
           String result = getIntent().getStringExtra("name");
           t1.setText(result);
        }
    }
}