MainActivity.java
package com.ample.receive;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
private TextView myTextView1;
private TextView myTextView2;
private CheckBox myCheckBox;
private Button myButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myCheckBox=(CheckBox)findViewById(R.id.myCheckBox);
myCheckBox.setChecked(false);
myCheckBox.setOnClickListener(this);
myButton=(Button)findViewById(R.id.myButton);
myButton.setEnabled(false);
myButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.myCheckBox:
if (myCheckBox.isChecked())
{
myButton.setEnabled(true);
myTextView2.setText("abc");
}
else
{
myButton.setEnabled(false);
myTextView1.setText(R.string.text);
myTextView2.setText(R.string.no);
}
break;
case R.id.myButton:
if (myCheckBox.isChecked())
{
myTextView1.setText(R.string.ok);
}
break;
default:break;
}
}
@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 boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
错误很明显,是空的对象,你的TextView在setText之前,要记得findviewbyId,否则就会出现这个错误。
myTextView1=(TextView)findViewById(R.id.myTextView1); myTextView2=(TextView)findViewById(R.id.myTextView2);把这两句放在onCreate里面就可以了。
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.ample.receive.MainActivity"
android:orientation="vertical" >
<TextView
android:id="@+id/myTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text"
android:textSize="20sp" />
<TextView
android:id="@+id/myTextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/myCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/agree"
android:textSize="20sp" />
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sure"
android:textSize="20sp" />
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">约定</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="text">加入本组织,就得服从组织上的安排</string>
<string name="agree">我同意</string>
<string name="sure">确定</string>
<string name="no">请勾选我同意</string>
<string name="ok">你已同意了</string>
</resources>
TextView组件没有初始化( myTextView1和myTextView2)
看log日志啊大兄弟,空指针。而且日志里面很清晰地写出了是TextView对象。。。
TextView没有初始化这是一点,CheckBox的事件是选中事件,你写成点击事件了