使用浮动按钮启动 Activity
Starting Activity with a Floatingbutton
所以我的列表 class 有一个浮动按钮。我想让它在我单击该按钮时进入我的另一个活动 MainActivity。这是我的列表 class.
的代码
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.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)
我该如何解决这个问题?
您需要在清单文件中声明您的 activity。
将此添加到您的清单中:
<activity android:name=".MainActivity" android:screenOrientation="portrait" />
您所有的 <activity... />
标签都应放在 <application.. />
标签下。
所以我的列表 class 有一个浮动按钮。我想让它在我单击该按钮时进入我的另一个活动 MainActivity。这是我的列表 class.
的代码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.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)
我该如何解决这个问题?
您需要在清单文件中声明您的 activity。
将此添加到您的清单中:
<activity android:name=".MainActivity" android:screenOrientation="portrait" />
您所有的 <activity... />
标签都应放在 <application.. />
标签下。