【问题标题】:Main Activity Error Variable cannot be found找不到主要活动错误变量
【发布时间】:2017-10-17 23:59:13
【问题描述】:

我收到错误“无法解析变量 'navList'”。我不明白为什么它找不到navList。如果有人能指出我哪里出错了,我将不胜感激。对不起菜鸟问题。

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {

/**
 * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
 */
private NavigationDrawerFragment mNavigationDrawerFragment;

/**
 * Used to store the last screen title. For use in {@link #restoreActionBar()}.
 */
private CharSequence mTitle;

private ListView mDrawerList;

private ArrayAdapter<String> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));

    mDrawerList = (ListView)findViewById(R.id.navList);

}

编辑 -

activity_main.xml

<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->

<!-- As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions. -->
<FrameLayout android:id="@+id/container" android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     If you're not building against API 17 or higher, use
     android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
     the container. -->
<fragment android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
    android:layout_gravity="start" android:name="com.yamini.nav.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

【问题讨论】:

  • 请发布你的activity_main.xml。可能是你的android:id 错误。
  • 发布你的xml代码activity_main.xml
  • 您的 XML 中没有 navList
  • mDrawerList = (ListView)findViewById(R.id.navList); 这一行你有错误。这意味着 id navList 在您的 listview 标签的 id 与 navList 不同的 activity_main.xml 文件中找不到。因此,使用navList 名称添加listview 标签的id。
  • 谢谢你们。我已经发布了我的 activity_main.xml

标签: java android navigation-drawer main


【解决方案1】:

您的 xml 文件中没有 navList

从代码中的行来看,您使用的是片段。

尝试从您的 NavigationDrawerFragment 调用代码,而不是在其根视图上。

// inside NavigationDrawerFragment class
mDrawerList = (ListView)MyRootView.findViewById(R.id.navList);

【讨论】:

  • @chocolateAndMath 我假设您将MyRootView 更改为您的实际根视图?你看到了什么错误?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多