Java Android文本视图
嗨,我是Android编程新手,我正在尝试制作一个简单的程序,通过点击一个按钮来改变文本。这是我的密码:Java Android文本视图,java,android,Java,Android,嗨,我是Android编程新手,我正在尝试制作一个简单的程序,通过点击一个按钮来改变文本。这是我的密码: public class HelloAndroidActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate
public class HelloAndroidActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button_scan = (Button) findViewById(R.id.button_scan);
button_scan.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonBeenPressed();
}
});
}
public void buttonBeenPressed(){
final Button button_scan = (Button) findViewById(R.id.button_scan);
TextView tv_barcode = (TextView)findViewById(R.id.textview_barcode);
if (tv_barcode != null){
tv_barcode.setText("been pressed.");
} else {
button_scan.setText("it's null dawg.");
}
}
}
和我的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:text="Scan" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/button_scan"></Button>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="TextView" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textview_barcode"></TextView>
</LinearLayout>
但是TextView返回NULL,我不知道为什么。有什么建议吗?谢谢。在我看来一切正常
调试此功能的最佳方法可能是在emulator或手机上运行应用程序,然后运行,找到文本视图并检查id。在我看来一切正常
调试此程序的最佳方法可能是在模拟器或手机上运行应用程序,然后运行,找到您的文本视图并检查id。Hmmm我对您的代码没有任何问题,运行时没有任何错误!您正在使用eclipse吗?然后尝试清理您的项目。嗯,我对您的代码没有任何问题,运行时没有任何错误!您正在使用eclipse吗?然后尝试清理项目。您可以使用文本视图或按钮。下面是我从中找到的一个文本视图示例
您可以使用文本视图或按钮。下面是我从中找到的一个文本视图示例
<TextView
android:text="@string/textview_onclick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview_onclick"
android:layout_below="@+id/textview_center"
android:textSize="25dp"
android:onClick="changeTextColor"
android:clickable="true"/>
public void changeTextColor (View view)
{
TextView textView = (TextView) view.findViewById(R.id.textview_onclick);
textView.setText("newWord");
}