Eclipse Android - Intent停止工作

时间:2015-03-13 19:42:15

标签: java android eclipse android-intent

所以在我的android项目中有意图停止工作,我不知道为什么,它之前工作,我检查是否只是意图工作,但它不是,当我试图只是改变按钮的文本通过点击它工作:

试图离开这里:

public class Level_Of_training extends Activity implements OnClickListener {

Intent i;
Button Beginner;
Button Intermediate;
Button Advanced;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_level__of_training);

    Beginner = (Button) findViewById(R.id.Beginner_bt);
    Intermediate = (Button) findViewById(R.id.Intermediate_bt);
    Advanced = (Button) findViewById(R.id.Advanced_bt);

    Beginner.setOnClickListener(this);
    Intermediate.setOnClickListener(this);
    Advanced.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.level__of_training, menu);
    return true;
}

@Override
public void onClick(View v) {



    boolean beginner        = v.getId() == this.Beginner.getId();
    boolean intermediate    = v.getId() == this.Intermediate.getId();
    boolean advanced        = v.getId() == this.Advanced.getId();

    if (beginner || intermediate || advanced) {
        if (beginner) {
            Global.questionare.setTraining_level(Global.TRAINING_LEVEL_BEGINNER);
        }
        else if (intermediate) {
            Global.questionare.setTraining_level(Global.TRAINING_LEVEL_INTERMEDIATE);
        }
        else if (advanced) {
            Global.questionare.setTraining_level(Global.TRAINING_LEVEL_ADVANCED);
        }

        SharedPreferences prefs = getSharedPreferences("com.example.shreddedacademy", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();

        editor.putInt("type",   Global.questionare.getBody_type());
        editor.putInt("goal",   Global.questionare.getTraining_goal());
        editor.putInt("level",  Global.questionare.getTraining_level());
        editor.putInt("workout",Global.questionare.getWorkout());

        editor.commit();

        i = new Intent();
        i.setClass(Level_Of_training.this, InfoActivity.class);
        startActivity(i);
    }
}

}

到这里:

public class InfoActivity extends Activity {
private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_info);

    listView = (ListView) findViewById(R.id.listView1);

    SharedPreferences prefs = getSharedPreferences("com.example.shreddedacademy", Context.MODE_PRIVATE);
    int type    = prefs.getInt("type",      -1);
    int fat     = prefs.getInt("fat",       -1);
    int goal    = prefs.getInt("goal",      -1);
    int level   = prefs.getInt("level",     -1);
    int workout = prefs.getInt("workout",   -1);

    if (type == -1 || fat == -1 || goal == -1 || level == -1 || workout == -1)
        this.finish();

    Workout w = Global.workouts[workout];

    WorkoutAdapterItem[] items = new WorkoutAdapterItem[w.Size() + w.get_excercises().size()];
    boolean next_is_header = true;
    int header_count = 0;
    int current_excercise = 0;

    for (int i = 0; i < items.length; i++) {
        if (next_is_header) {
            items[i] = new WorkoutAdapterItem(String.valueOf(Character.toChars('A' + header_count)), 30);
            header_count++;
            current_excercise = 0;
            next_is_header = false;
        } else {
            items[i] = new WorkoutAdapterItem(w.get_excercise(header_count - 1, current_excercise), 20);
            current_excercise++;
            if (current_excercise == w.get_excercises().get(header_count - 1).size()) {
                next_is_header = true;
            }
        }
    }

    CustomAdapter cadapter = new CustomAdapter(this, R.layout.list_view_item, items);
    listView.setAdapter(cadapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.info, menu);
    return true;
}

}

3 个答案:

答案 0 :(得分:0)

试试这个:

    i = new Intent(Level_Of_training.this, InfoActivity.class);
    startActivity(i);

(用上面的替换Level_of_training类的底部)

答案 1 :(得分:0)

你可以试试这个:

Intent in = new Intent(getApplicationContext(), InfoActivity.class); startActivity(in);

答案 2 :(得分:0)

好吧,看起来你的状况并不令人满意,我只能猜测由于说明不充分,试着提出突破点并找出错误到位的地方。