繁体   English   中英

无法在Android中打开活动

[英]Can't open an activity in android

我在android编程中遇到一些问题。 我试图在手机上打开活动,但失败。 它说很不幸,该程序已停止。 我不知道如何解决此问题,我尝试通过下面的方法进行连接。 任何帮助表示赞赏。 请询问我是否指定的不够。

    public void addGlossary(View v){
    Intent intent = new Intent (this, addGlossary.class);
    Button buttonZero = (Button) findViewById(R.id.buttonZero);
    startActivity(intent);

}

这是活动的代码:

public abstract class addGlossary extends Activity implements OnClickListener, OnInitListener {


    private int MY_DATA_CHECK_CODE = 0;


   private TextToSpeech myTTS;


private MediaPlayer mMediaPlayer;


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


        Button speakButton = (Button)findViewById(R.id.speak);
        speakButton.setOnClickListener(this);


        Intent checkTTSIntent = new Intent();
        checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer = MediaPlayer.create(this, R.raw.button);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.start();
        mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mMediaPlayer.stop();
            }
        });


    }

    public void buttonReturn(View v){
        Intent intent = new Intent (this, MainActivity.class);
        Button buttonreturn = (Button) findViewById(R.id.buttonReturn);
        startActivity(intent);

    }


    public void startGame(View v){
        Intent intent = new Intent (this, firstLevel.class);
        Button buttonstart = (Button) findViewById(R.id.buttonStart);
        startActivity(intent);



    }




    public void onInit(int initStatus) {
        if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE) {
            myTTS.setLanguage(Locale.US);
        }

        else if (initStatus == TextToSpeech.ERROR) {
            Toast.makeText(this, "Sorry! Text To Speech failed...", Toast.LENGTH_LONG).show();
        }

    }


    private void speakWords(String speech){
        myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);



        }

    public void buttonAdd(){
        //lagrar texten från textfältet man knappade in till string
        EditText enteredText = (EditText)findViewById(R.id.enter);
        String words = enteredText.getText().toString();
        speakWords(words);

    }



    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MY_DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                myTTS = new TextToSpeech(this, this);
            }
            else {
                Intent installTTSIntent = new Intent();
                installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installTTSIntent);
            }
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_add_glossary, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

您的adGlossary类是抽象类。 Android需要看到一个具体的Activity实施才能开始。

删除abstract关键字,并假设清单中有活动,则该活动应该开始。

**编辑:**正如克里斯·斯特拉顿(Chris Stratton)在评论中指出的那样,放置摘要的可能原因是围绕实现OnClickListener和OnInitListener。 尽管代码可能已经编译,但是该解决方案仍然不可行。

因此,除了删除抽象限定符之外,您还必须实现接口。

解决方案:删除abstract修饰符。 编译器应立即检测到未实现的方法,并应要求您提供添加未实现的方法的选项,或将类标记为抽象(红色蠕动的线条将显示出来,并且将鼠标移到上面时,应获得这些选项)。 要求编译器添加未实现的方法。 您现在应该拥有:

@Override
public void onClick(View view) {
    // Do something when clicked
    // Implement logic here
}

@Override
public void onInit(int status) {
    // Implement logic here
}

// It will be all done and everything should work

只需在上述方法中实现您的逻辑即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM