将按钮链接到布局

时间:2013-12-19 11:06:47

标签: android eclipse

我想在eclipse中将按钮链接到另一个布局。当我按下按钮时,MainActivity.java中的此功能运行:

public void press(View v)
{
Intent intent = new Intent(this, GolPooch.class);
startActivity(intent);
}

我已经在GolPooch.java中编写了这段代码:

    package com.example.chance;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;


public class GolPooch extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.golpooch_layout);
    }

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

}

但按下按钮时出现运行时错误!我搜索了很多,但我一无所获。为什么它不起作用?

3 个答案:

答案 0 :(得分:1)

我想我发现了这个问题。

Intent intent = new Intent(this, GolPooch1.class);

您在这里调用名为GolPooch1的类,但您创建的文件是GolPooch.java因此存在不匹配且失败。

第二个选项:

您是否在清单中定义了新活动?如果没有,先定义它,然后清理并构建一个项目。

答案 1 :(得分:1)

将以下内容添加到清单中:

<activity android:name=".GolPooch" />

答案 2 :(得分:0)

我认为你必须写

  Intent intent= new Intent(getApplicationContext(), GolPooch.class);
  startActivity(intent);

并在清单文件中定义您的GolPooch.java类。

  <activity android:name=".GolPooch" />