我的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");
}
}
}
答案 0 :(得分:1)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
将这些移动到OnCreate的开头,您的应用将开始工作
答案 1 :(得分:0)
按照以下方式制作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);
...