我正在尝试编写一个程序,该程序在单击下一个按钮时显示一个字符串“hello”。 我已经采取了两项活动。当我运行我得到这个日志猫。我是新的android编程请帮助我。Android活动 - 无法实例化活动组件信息
我的主要活动是,
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.trailcarehand.MESSAGE";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Button button =(Button)findViewById(R.id.btnNext);
public void onClick(View view)
{
Intent i=new Intent(this,DisplayMessageActivity.class);
String message = "hello";
i.putExtra(EXTRA_MESSAGE, message);
startActivity(i);
//count++;
}
}
我displaymessage活动
public class DisplayMessageActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent i = getIntent();
String message = i.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(R.layout.activity_main);
}
我activity_main.xml中是
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="26dp"
android:paddingBottom="5dp"
android:text=" welcome "
android:textColor="#f7f9f6"
android:textSize="13dp"
android:textStyle="italic" />
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView1"
android:onClick="onClick"/>
+0
您必须在oncreate中调用findViewById,并且您还没有将onclick方法连接到任何位置的按钮。请参阅View.setOnClickListener –