0

我有一个初始化的按钮

button1= (Button)findViewById(R.id.button1);

我有 OnClick() 函数

public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(this, MainActivity2.class);
        startActivity(intent);

    }

我的新活动的名称是清单文件中的 MainActivity2

  <activity
            android:name="com.example.intent.MainActivity2"
            android:label="@string/title_activity_main_activity2" >
        </activity>

但是第二个活动永远不会启动

请帮忙。

活动一

public class MainActivity extends Activity implements OnClickListener {

    Button button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1= (Button)findViewById(R.id.button1);

    }


    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(this, MainActivity2.class);
        startActivity(intent);

    }


}

XML 布局

<activity
            android:name="com.example.intent.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.intent.MainActivity2"
            android:label="@string/title_activity_main_activity2" >
        </activity>
    </application>
4

3 回答 3

1

您没有将onClick事件注册到按钮。

从:

public class MainActivity extends Activity implements OnClickListener {

    Button button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1= (Button)findViewById(R.id.button1);

    }
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(this, MainActivity2.class);
        startActivity(intent);
    }
}

至:

public class MainActivity extends Activity implements OnClickListener {

    Button button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1= (Button)findViewById(R.id.button1);
        button1.setOnClickListener(this);
    }
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(this, MainActivity2.class);
        startActivity(intent);
    }
}
于 2013-03-21T10:56:58.570 回答
0

哥们,你只需要从你的代码中改变一个词..

public class MainActivity extends Activity implements OnClickListener {

Button button1;

public void onClick(View arg0) {
    // TODO Auto-generated method stub
    if (R.id.button1) {
    Intent intent = new Intent(MainActivity.this, MainActivity2.class);
    startActivity(intent);
    }
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button1= (Button)findViewById(R.id.button1);
    button1.setOnClickListener(this);
}

这肯定会奏效....

于 2013-03-21T11:12:54.210 回答
0

改变

public void onClick(View arg0) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(this, MainActivity2.class);
    startActivity(intent);

}

button1.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(this, MainActivity2.class);
    startActivity(intent);
   }
};

您应该将 onClicklistener 设置为按钮

于 2013-03-21T10:53:50.287 回答