使用浮动按钮启动活动

时间:2016-06-15 15:36:53

标签: android button floating-action-button start-activity

所以我的List类有一个浮动按钮。我想做到这一点,当我点击那个按钮时,它进入我的另一个活动,MainActivity。这是我列表类的代码。

public class List extends Activity {
ImageButton floatButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
    populateUsersList();

    floatButton = (ImageButton) findViewById(R.id.imageButton);
    floatButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent barcodes = new Intent (List.this, MainActivity.class);
            startActivity(barcodes);
        }
    });
}

private void populateUsersList() {
    // Construct the data source
    ArrayList<User> arrayOfUsers = User.getUsers();
    // Create the adapter to convert the array to views
    UserAdapter adapter = new UserAdapter(this, arrayOfUsers);
    // Attach the adapter to a ListView
    ListView listView = (ListView) findViewById(R.id.lvUsers);
    listView.setAdapter(adapter);
}

它似乎应该可以工作,但每次我点击按钮时,都会弹出此错误代码。

06-15 11:34:46.299 2971-2971/com.example.pethoalpar.zxingexample E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.example.pethoalpar.zxingexample, PID: 2971
                                                                               android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.pethoalpar.zxingexample/apn.keychains.MainActivity}; have you declared this activity in your AndroidManifest.xml?
                                                                                   at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1777)
                                                                                   at android.app.Instrumentation.execStartActivity(Instrumentation.java:1501)
                                                                                   at android.app.Activity.startActivityForResult(Activity.java:3745)
                                                                                   at android.app.Activity.startActivityForResult(Activity.java:3706)
                                                                                   at android.app.Activity.startActivity(Activity.java:4016)
                                                                                   at android.app.Activity.startActivity(Activity.java:3984)
                                                                                   at apn.keychains.List$1.onClick(List.java:28)
                                                                                   at android.view.View.performClick(View.java:4780)
                                                                                   at android.view.View$PerformClick.run(View.java:19866)
                                                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                   at android.os.Looper.loop(Looper.java:135)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

您需要在清单文件中声明您的活动。

将此添加到您的清单:

<activity android:name=".MainActivity" android:screenOrientation="portrait" />

您的所有<activity... />代码都应放在<application.. />代码下。