我是Android Studio的初学者。我正在使用Windows。我的主要活动出错了。错误:在android中非法启动表达式。请帮我解决这个问题。提前谢谢。
守则
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button Expense=(Button)findViewById(R.id.Expense);
Button Income=(Button) findViewById(R.id.Income);
Expense.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Addpanel.class);
startActivity(intent);
}
});
Income.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent(MainActivity.this, Addincome.class);
startActivity(intent1);
}
});
public class MainActivity extends ActionBarActivity {
DatabaseHelper myDb;
EditText editproduct,editamount;
Button btnsave;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addpanel);
myDb = new DatabaseHelper(this);
editproduct = (EditText)findViewById(R.id.product);
editamount = (EditText)findViewById(R.id.amount);
btnsave = (Button)findViewById(R.id.save);
save();
}
public void save() {
btnsave.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isInserted = myDb.insertData(editproduct.getText().toString(),
editamount.getText().toString()
);
if(isInserted == true)
Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
}
}
);
}
public void showMessage(String title,String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
}
}
答案 0 :(得分:1)
您已在代码中创建了两个MainActivity,因此您会收到非法启动表达式的错误。您必须在单一类Mainactivity中编写代码。
答案 1 :(得分:1)
您有两个MainActivity,可能导致错误。试着摆脱其中一个。
答案 2 :(得分:0)
在一个类中有两个活动需要为两个MainActivity创建两个单独的类。