0

こんにちは、Android dev の初心者です。次のコードでエラーが発生します。 .onCreate ですが、このエラーが発生していますか?

    public class DisplayMessageActivity {

        @SuppressLint("NewApi")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);

                // Get the message from the intent
                Intent intent = getIntent();
                String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

                        // Create the text view
                TextView textView = new TextView(null);
                textView.setTextSize(40);
                textView.setText(message);

                // Set the text view as the activity layout
                setContentView(textView);

            // Make sure we're running on Honeycomb or higher to use ActionBar APIs
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                // Show the Up button in the action bar.
                ((Object) getActionBar()).setDisplayHomeAsUpEnabled(true);
            }
    }
        private void setContentView(TextView textView) {
            // TODO Auto-generated method stub

        }
        private Intent getIntent() {
            // TODO Auto-generated method stub
            return null;
        }
        private Object getActionBar() {
            // TODO Auto-generated method stub
            return null;
        }
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

    }
4

3 に答える 3

0

変化する: protected void onCreate(Bundle savedInstanceState)

に: public void onCreate(Bundle savedInstanceState)

私は同じチュートリアルに取り組んでいました。チュートリアルからコードを直接コピーし、アプリを実行しました (動作しました)。違いを確認するために出発しましたが、これが私が見つけた唯一のものです。

それが役に立てば幸い!

ハッピーコーディング!!

于 2014-10-19T09:57:51.130 に答える
0

@StinePikeの回答は、差し迫った問題を解決します。ただし、 Intentについても読む必要があります。これらは基本的にメッセージを送信するための単なる方法です。これは、何かを開始するのではなく、何をすべきか、特定のデータをどのように処理するかを何かに指示するという点で、暗黙のアクションです。私がこれを言うのは、あなたが という関数を持っているという点で誤解されていると思うからですgetIntent()。これはクラスの関数である必要はありませんがActivityIntent.

あなたがいいねを始めたとしましょうActivity

Intent intent = new Intent(MyActivity.this, NextActivity.class);  // MyActivity is the Activity you are currently in and NextActivity is the Activity you are starting
intent.putExtra("people", "Fred");  // This isn't necessary but a way to pass data. I put "Fred" but this could be a String variable or something else
startActivity(intent);

あなたNextActivityがするかもしれません:

@Override
public void onCreate(Bundle bundle)
{
    Intent recIntent = getIntent();  // this gets the intent information sent from MyActivity
    String data = recIntent.getStringExtra("people");  // data now equals  the String Fred
}

という関数を作成する必要はありませんgetIntent()。これはネイティブ関数です。したがって、これを使用するときgetIntent()は、nullこれActivityIntent. ランチャーだったかもActivity

于 2013-04-18T23:19:07.623 に答える