0

嗨,我是 android dev 的新手,以下代码给出错误 1) 对象类型的 onCreate(bundle) 方法未定义 2) DisplayMessageActivity 类型的方法 onCreate(bundle) 必须被覆盖或实现我正在使用的超类型方法 super .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

4

您忘记扩展 Activity 类。

public class DisplayMessageActivity extends Activity

如果要创建 Activity,则必须将 Activity 扩展为超类。要了解有关活动基础的更多信息,请参阅此处的文档

于 2013-04-18T22:53:32.290 回答
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 回答
0

改变: protected void onCreate(Bundle savedInstanceState)

至: public void onCreate(Bundle savedInstanceState)

我正在研究相同的教程。我直接从教程中复制了代码并运行了应用程序(有效)。所以开始检查差异,这是我发现的唯一一个。

希望能帮助到你!

编码快乐!!

于 2014-10-19T09:57:51.130 回答