Reputation: 1
I am a beginner in Android Studio. I am using Windows. I am getting error on my main activity. error: illegal start of expression in android.Please help me to fix this problem.Thanks in advance.
The Code is
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);
}
}
}
}
Upvotes: 0
Views: 151
Reputation: 534
there are two activity in one class you need to create two seprated class for both MainActivity.
Upvotes: 1
Reputation: 131
You have two MainActivity, that could be causing the error. Try getting rid of the one of them.
Upvotes: 1
Reputation: 83
you have created two MainActivity in your code so you get error of illegal start of expression. You have to write your code in single class Mainactivity.
Upvotes: 1