我构建了一个程序,其主页面现在包含两个按钮。第二个按钮不起作用。但是第一个按钮启动活动。 第二个是" btninfo"。 (InfoActivity在manifest.xml中定义) 我已经在下面写了MainActivity的代码:
package com.hesamapps.build;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.view.View.*;
import android.content.*;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.btnmain);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
Intent i = new Intent(MainActivity.this, AmazingActivity.class);
startActivity(i);
}
});
Button btn2 = (Button) findViewById(R.id.btninfo);
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
Intent a = new Intent(MainActivity.this,InfoActivity.class);
startActivity(a);
}
});
}
和下面的InfoActivity:
package com.hesamapps.build;
import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;
public class InfoActivity extends Activity
{
@Override
public void onStart()
{
// TODO: Implement this method
super.onStart();
setContentView(R.layout.info);
}
}
按钮必须启动InfoActivity,但它会强制关闭错误。 (按钮是" btninfo")