代码不工作(android)

时间:2014-05-24 10:07:54

标签: java android eclipse

我知道这是一个非常业余的问题,但我真的很沮丧: 我没有收到错误,但它没有工作。(忽略包部分)包com.example.project1; import android.app.Activity;

这是java文件

 package com.example.project1;

 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.TextView;

public class Tictactoe2 extends Activity implements OnClickListener{
EditText et1 , et2; 
String str1 , str2;
Button b1;
TextView tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    super.onCreate(savedInstanceState);
    setContentView(R.layout.ttt);
    et1 = (EditText) findViewById (R.id.et1);
    et2 = (EditText) findViewById (R.id.et2);
    tv2 = (TextView) findViewById (R.id.tv2);
    str1 = et1.getText().toString();
    str2 = et2.getText().toString();
    b1 = (Button) findViewById (R.id.b1);

    b1.setOnClickListener(this);


}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {

    case R.id.b1:
        tv2.setText(str1);
    /*Bundle bun = new Bundle();
    bun.putString("key", str1);
    bun.putString("key1", str2);;
    Intent intent = new Intent(tictactoe2.this , tictactoe.class);
    intent.putExtras(bun); 
    startActivity(intent);*/
    break;
}}

}

3 个答案:

答案 0 :(得分:1)

我猜你得到了NPE。你需要删除这一行

 str1 = et1.getText().toString();
 str2 = et2.getText().toString();

这是因为您正在尝试getText() EditText并且其中没有文字。因此,我建议您将这一行移到Listener events下的EditText之后,在@Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()) { case R.id.b1: str1 = et1.getText().toString(); str2 = et2.getText().toString(); tv2.setText(str1); }} 中设置值后,您会获得值。

你实现像

{{1}}

答案 1 :(得分:0)

 package com.example.project1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Tictactoe2 extends Activity implements OnClickListener{
EditText et1 , et2; 
String str1 , str2;
Button b1;
TextView tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub

super.onCreate(savedInstanceState);
setContentView(R.layout.ttt);
et1 = (EditText) findViewById (R.id.et1);
et2 = (EditText) findViewById (R.id.et2);
tv2 = (TextView) findViewById (R.id.tv2);

b1 = (Button) findViewById (R.id.b1);

b1.setOnClickListener(this);


}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{

case R.id.b1:
str1 = et1.getText().toString();
str2 = et2.getText().toString();
    tv2.setText(str1);
/*Bundle bun = new Bundle();
bun.putString("key", str1);
bun.putString("key1", str2);;
Intent intent = new Intent(tictactoe2.this , tictactoe.class);
intent.putExtras(bun); 
startActivity(intent);*/
break;
}}

}

答案 2 :(得分:0)

你应该在你的onClick中放置你的“str1”字符串和textview以及edittext代码。

像这样:

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{

case R.id.b1:
et1 = (EditText) findViewById (R.id.et1);
tv2 = (TextView) findViewById (R.id.tv2);
str1 = et1.getText().toString();
    tv2.setText(str1);
/*Bundle bun = new Bundle();
bun.putString("key", str1);
bun.putString("key1", str2);;
Intent intent = new Intent(tictactoe2.this , tictactoe.class);
intent.putExtras(bun); 
startActivity(intent);*/
break;

}}