Android 无法通过单击图像按钮启动活动
我已经为我的动作栏定制了一个主题,这样它就可以在屏幕顶部显示三个按钮。点击最左边的按钮,我想开始一个新的活动。但是,我不能这样做。我已经使用了正确的方法开始了一项活动,但仍然出现了一个错误。我不知道是什么问题 到目前为止我编写的代码 MainActivity.javaAndroid 无法通过单击图像按钮启动活动,android,android-layout,android-intent,Android,Android Layout,Android Intent,我已经为我的动作栏定制了一个主题,这样它就可以在屏幕顶部显示三个按钮。点击最左边的按钮,我想开始一个新的活动。但是,我不能这样做。我已经使用了正确的方法开始了一项活动,但仍然出现了一个错误。我不知道是什么问题 到目前为止我编写的代码 MainActivity.java package com.example.contactmanager1; import java.util.ArrayList; import java.util.List; import android.app.Activ
package com.example.contactmanager1;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView listView;
private ImageButton button1;
private ImageButton button2;
private ImageButton button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setHomeButtonEnabled(false);
getActionBar().setDisplayShowCustomEnabled(true);
getActionBar().setCustomView(R.layout.button_layout);
getActionBar().setDisplayShowHomeEnabled(false);
listView = (ListView)findViewById(R.id.main_contact_listview);
button1= (ImageButton)findViewById(R.id.button_search);
button2= (ImageButton)findViewById(R.id.button_addcontact);
button3= (ImageButton)findViewById(R.id.button_options);
setUpListView();
}
private void setUpListView(){
List <String> displayList = new ArrayList<String>();
displayList.add("Display Item 1");
displayList.add("Display Item 2");
displayList.add("Display Item 3");
ListAdapter listAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,displayList);
listView.setAdapter(listAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
//Handle presses on the action bar items
switch(item.getItemId()){
case R.id.action_button_groups:
Intent intent = new Intent(this,Groups.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void addContact(View view){
Intent intent = new Intent(this,AddContact.class);
startActivity(intent);
}
public void groupPage(View view){
Intent intent = new Intent(this,Groups.class);
startActivity(intent);
}
}
package com.example.contactmanager1;
导入java.util.ArrayList;
导入java.util.List;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.ImageButton;
导入android.widget.ListAdapter;
导入android.widget.ListView;
公共类MainActivity扩展了活动{
私有列表视图列表视图;
私有图像按钮1;
私有图像按钮2;
私有图像按钮3;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setHomeButtonEnabled(false);
getActionBar().setDisplayShowCustomEnabled(true);
getActionBar().setCustomView(R.layout.button_布局);
getActionBar().setDisplayShowHomeEnabled(false);
listView=(listView)findViewById(R.id.main\u contact\u listView);
button1=(ImageButton)findViewById(R.id.button\u搜索);
button2=(ImageButton)findViewById(R.id.button\u addcontact);
button3=(ImageButton)findViewById(R.id.button\u选项);
setUpListView();
}
私有void setUpListView(){
List displayList=new ArrayList();
显示列表。添加(“显示项1”);
显示列表。添加(“显示项2”);
显示列表。添加(“显示项目3”);
ListAdapter ListAdapter=new ArrayAdapter(MainActivity.this,
android.R.layout.simple\u list\u item\u 1,displayList);
setAdapter(listAdapter);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
返回true;
}
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目上的按下
开关(item.getItemId()){
案例R.id.行动按钮组:
意向意向=新意向(此,Groups.class);
星触觉(意向);
返回true;
违约:
返回super.onOptionsItemSelected(项目);
}
}
public void addContact(视图){
Intent Intent=新Intent(这个,AddContact.class);
星触觉(意向);
}
公共无效组页(视图){
意向意向=新意向(此,Groups.class);
星触觉(意向);
}
}
button_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@android:color/holo_blue_dark"
android:padding="5dp"
android:layout_weight="1">
<ImageButton
android:id="@+id/action_button_groups"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/groups"
android:onClick="groupPage" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@android:color/white"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@android:color/holo_blue_dark"
android:padding="5dp"
android:layout_weight="1">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/contactlist" />
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@android:color/white"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@android:color/holo_blue_dark"
android:padding="5dp"
android:layout_weight="1">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/favourites" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
对于button_layout.xml中的第一个图像按钮,我添加了一个onClick属性,指向方法groupPage。我已在MainActivity.java中定义了此方法。带有imagebutton的listView会导致问题,因为它会消耗对它的所有接触。因此,请尝试使用imageView替换imagebutton或尝试此链接。
我想启动一个新的活动。但是,我不能这样做。
-单击按钮后会得到什么?布局中没有列表视图。我测试了您的代码,并删除了操作栏和列表视图的代码。这个问题中的布局是动作栏的自定义布局。不是主要活动