我正在尝试在主要活动中为菜单项充气,但我无法在主要活动中创建菜单。
这是我的Java代码。
package in.webxstudio.classesfinal;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private ImageView profile;
private Intent intent;
private TextView name,course,roll_no,total_lecture,lecture_attended,exams_conducted,exams_appeared,total_lessons,lessons_covered;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeVariables();
profile=findViewById(R.id.profile_image);
Glide.with(getApplicationContext()).asBitmap().load("https://i.imgur.com/cXuNKOA.jpg").into(profile);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_page_menu, menu);
// return true so that the main_page_menu pop up is opened
return true;
}
private void initializeVariables(){
name=findViewById(R.id.student_name);
course=findViewById(R.id.course);
roll_no=findViewById(R.id.roll_no);
total_lecture=findViewById(R.id.total_lecture_text);
lecture_attended=findViewById(R.id.total_lecture_attended_text);
exams_conducted=findViewById(R.id.exams_conducted_text);
exams_appeared=findViewById(R.id.exams_appeared_text);
total_lessons=findViewById(R.id.total_lessons_text);
lessons_covered=findViewById(R.id.lessons_covered_text);
}
private void setData(List<String> personal,List<String> attendance,List<String> results,List<String> progress){
name.setText(personal.get(0));
course.setText(personal.get(1));
roll_no.setText(personal.get(2));
Glide.with(getApplicationContext()).asBitmap().load(personal.get(3)).into(profile);
total_lecture.setText(attendance.get(0));
lecture_attended.setText(attendance.get(1));
exams_conducted.setText(results.get(0));
exams_appeared.setText(results.get(1));
total_lessons.setText(progress.get(0));
lessons_covered.setText(progress.get(1));
}
private void gotoDestination(Class classname){
intent=new Intent(this,classname);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
public void Attendance_Page(View v){
gotoDestination(Attendance_report.class);
}
public void ResultPage(View v){
gotoDestination(Result_report.class);
}
public void ProgressPage(View v){
gotoDestination(Progress_Activity.class);
}
}
这是我的菜单
的Xml代码<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/settings_menu"
android:title="@string/settings" />
<item
android:id="@+id/log_out_menu"
android:title="@string/log_out"/>
</menu>
我还应该做些什么。 我在我的Oncreate方法下面创建了onCreateOptionMenu()。 请解释我的代码无效的原因以及代码中的错误
答案 0 :(得分:1)
您需要在oncreate中初始化工具栏,并使用setSupportActionBar(工具栏)将其设置为工具栏