2

我正在创建一个菜单,每侧有 2 个导航抽屉:

左/右导航抽屉

一切正常,但现在我想将填充任务从主要活动分离到左活动和右活动,所以我已经分离了布局(只是为了更好的结构)

这是主要布局:

<!-- The main content view -->

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/main_list_viewer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:divider="@android:color/darker_gray"
        android:dividerHeight="1dp"
        android:footerDividersEnabled="true"
        android:paddingLeft="1dp" >
    </ListView>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/hello_world" />
</RelativeLayout>

<!-- The left navigation drawer -->

<include layout="@layout/activity_left" />

<!-- The right navigation drawer -->

<include layout="@layout/activity_right" />

左侧抽屉布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#111"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/buttons_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/login_image_button"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:contentDescription="@string/login_image"
        android:src="@drawable/ic_launcher" />

    <ImageButton
        android:id="@+id/logout_image_button"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:contentDescription="@string/login_image"
        android:src="@drawable/ic_launcher" />
</LinearLayout>

<ListView
    android:id="@+id/left_list_viewer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:choiceMode="singleChoice"
    android:divider="@android:color/black"
    android:dividerHeight="0dp" >
</ListView>

现在我用一些假人填充了左侧导航抽屉列表查看器。

public class LeftActivity extends SherlockActivity implements OnClickListener{
private ListView leftListView;
private ArrayAdapter<String> listAdapter;
// Session Manager Class
SessionManager session;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_left);
    moveTaskToBack(true);
    leftListView = (ListView) findViewById(R.id.left_list_viewer);

    // Create and populate a List of planet names.
    String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
            "Jupiter", "Saturn", "Uranus", "Neptune" };
    ArrayList<String> planetList = new ArrayList<String>();
    planetList.addAll(Arrays.asList(planets));

    // Create ArrayAdapter using the planet list.
    listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow,
            planetList);

    // Add more planets. If you passed a String[] instead of a List<String>
    // into the ArrayAdapter constructor, you must not add more items.
    // Otherwise an exception will occur.
    listAdapter.add("Ceres");
    listAdapter.add("Pluto");
    listAdapter.add("Haumea");
    listAdapter.add("Makemake");
    listAdapter.add("Eris");

    // Set the ArrayAdapter as the ListView's adapter.
    leftListView.setAdapter(listAdapter);

    // OnClickListener's for all button, after pressed it will send for the
    // onClick method the button pressed
    ImageButton loginImageButton = (ImageButton) findViewById(R.id.login_image_button);
    loginImageButton.setOnClickListener(this);
    ImageButton logouImageButton = (ImageButton) findViewById(R.id.logout_image_button);
    logouImageButton.setOnClickListener(this);

}

// Method to decide what to do from what button was pressed
public void onClick(View v) {
    session = new SessionManager(getApplicationContext());
    switch (v.getId()) {
    case R.id.login_image_button:
        if (session.isLoggedIn() == false) {
            Intent intent = new Intent(getApplicationContext(),
                    LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        } else {
            Log.d("MoodyDebud",
                    "Entrará aqui se o utilizador ja estiver logado e em vez de vir para aqui irá para as defeniçoes de utilizador");
        }
        break;
    case R.id.logout_image_button:
        if (session.isLoggedIn() == true) {
            session.logoutUser();
        } else {
            // só entra neste else caso o utilizador ainda nao esteja
            // loggado entao é reencaminhado para o LoginActivity
            Intent intent = new Intent(getApplicationContext(),
                    LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
        break;
    default:
        throw new RuntimeException("Unknown button ID");
    }
}

}

但我的问题是当我启动 mainActivity 时 leftActivity 它没有初始化并且列表视图是空的,我认为它不是一个选项,因为它会在主菜单上。任何想法如何解决这个问题?提前致谢 ;)

4

0 回答 0