我的android程序代码没有启动并显示消息"不幸的是,"projectname"已经停止"
我刚刚用android eclipse编写了我的第一个程序,但java代码中似乎有一个错误。有人能告诉我出了什么错吗??
package com.example.first;
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.TextView;
public class MainActivity extends Activity implements OnClickListener{
private Button plus,minus ;
private TextView number;
@Override
protected void onCreate(Bundle savedInstanceState) {
plus= (Button) findViewById(R.id.plus);
minus=(Button) findViewById(R.id.minus);
number= (TextView) findViewById(R.id.textView1);
plus.setOnClickListener(this);
minus.setOnClickListener(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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);
}
public void onClick(View v)
{
if(v.getId() == R.id.plus)
{
number.setText("Plus");
}
else if(v.getId() == R.id.minus)
{
number.setText("Minus");
}
}
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
将这些移动到OnCreate的启动位置。您的应用程序将开始工作
让你的onCreate方法像这样:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
plus= (Button) findViewById(R.id.plus);
minus=(Button) findViewById(R.id.minus);
number= (TextView) findViewById(R.id.textView1);
...