gpt4 book ai didi

java - FindViewByID 在 Fragment Navigation Drawer 上返回 null

转载 作者:行者123 更新时间:2023-11-29 21:00:24 28 4
gpt4 key购买 nike

当我尝试将 onclicklistener 设置到我的 applybutton 时,出现以下错误。我使用这样的抽屉导航:http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

按钮在我的应用程序的设置 fragment 中。

<br/>
10-10 16:21:05.506 5641-5641/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: eu.twenty1media.smokestop, PID: 5641
ava.lang.RuntimeException: Unable to start activity ComponentInfo{eu.twenty1media.smokestop/eu.twenty1media.smokestop.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.view.View.setOnClickListener(android.view.View$OnClickListener)'
on a null object reference

这是 oncreate 完成之前 Mainactivity 的代码。

public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

// nav drawer title
private CharSequence mDrawerTitle;

// used to store app title
private CharSequence mTitle;

// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;

private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


/**
* Set onClickListener for Settings Apply Button
*/
findViewById(R.id.applysettings).setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
//Inform the user the button has been clicked
Toast.makeText(MainActivity.this, "Apply clicked.", Toast.LENGTH_SHORT).show();
}
});


//Initalize or create Database
DatabaseHandler db = new DatabaseHandler(this);
db.getData();
//db.addData();
//db.updateData();



mTitle = mDrawerTitle = getTitle();

// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

navDrawerItems = new ArrayList<NavDrawerItem>();

// adding nav drawer items to array
// Home
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Motivation
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Health
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
// Statistic
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
// Settings
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
//Welcome
//navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));


// Recycle the typed array
navMenuIcons.recycle();

mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);

// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}

public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);

if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}

} </code>

主 Activity .xml

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<!-- Listview to display slider menu -->
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"
android:background="@color/list_background"/>
</android.support.v4.widget.DrawerLayout>

和 SettingsFragment.xml

<?xml version="1.0" encoding="utf-8"?>

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginBottom="8dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Speichern"
android:id="@+id/applysettings"
/>

最佳答案

您的 applysettings View 不在 activity_main 布局中,因此无法找到。

findViewById() 移动到您实际展开包含 applysettings 按钮的布局的位置。

关于java - FindViewByID 在 Fragment Navigation Drawer 上返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26301883/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com