简体   繁体   中英

Setting onClickListener in navigation drawer fragment causing application to crash

The application crashes when I add onclick listener to navigation drawer fragment, but everything works fine when i remove it:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    myView= inflater.inflate(R.layout.fragment_adminproducts, container, false);
    proName=(EditText)myView.findViewById(R.id.proName);
    color=(EditText)myView.findViewById(R.id.color);
    quantity=(EditText)myView.findViewById(R.id.quantity);
    price=(EditText)myView.findViewById(R.id.price);
    btAdd=(Button)myView.findViewById(R.id.btAdd);

    btAdd.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            addProduct();

        }});


    return myView;

}

Logcat shows this NullPointException:

Caused by: java.lang.NullPointerException at com.hscs.ibd.ibdsystem.adminProducts.onCreateView(adminProducts.java:48) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501) at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)

etc.

Update: Drawer CLass: package com.hscs.ibd.ibdsystem;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class adminmain extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_adminmain);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    Fragment myFragment= new adminProducts();
    android.support.v4.app.FragmentManager fragmentManager= getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.contentStuff,myFragment)
            .commit();
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.adminmain, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    Fragment myFragment;

    if (id == R.id.nav_products) {
        myFragment = new adminProducts();
    } else if (id == R.id.nav_employee) {
        myFragment = new adminEmployee();
    } else if (id == R.id.nav_acc) {

    } else if (id == R.id.nav_logout) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

XML file:

<!-- TODO: Update blank fragment layout -->

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:inputType="textCapSentences"
        android:singleLine="true"
        android:imeOptions="actionNext"
        android:id="@+id/proName"
        android:hint="Product"
        android:textSize="16dp"
        android:background="@drawable/back"
        android:paddingLeft="10dp"
        android:textColor="#9e9e9e"
         />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:inputType="textCapSentences"
        android:singleLine="true"
        android:imeOptions="actionNext"
        android:id="@+id/color"
        android:hint="Color"
        android:textSize="16dp"
        android:background="@drawable/back"
        android:paddingLeft="10dp"
        android:textColor="#9e9e9e"
        android:layout_marginTop="20dp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:inputType="number"
        android:singleLine="true"
        android:imeOptions="actionNext"
        android:id="@+id/quantity"
        android:hint="Quantity"
        android:textSize="16dp"
        android:background="@drawable/back"
        android:paddingLeft="10dp"
        android:textColor="#9e9e9e"
        android:layout_marginTop="20dp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:inputType="number"
        android:singleLine="true"
        android:imeOptions="actionNext"
        android:id="@+id/price"
        android:hint="Price"
        android:textSize="16dp"
        android:background="@drawable/back"
        android:paddingLeft="10dp"
        android:textColor="#9e9e9e"
        android:layout_marginTop="20dp" />

    <Button
        android:layout_width="216dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="40dp"
        android:text="Add Product"
        android:id="@+id/btadd"
        android:backgroundTintMode="src_in"
        android:background="@drawable/button"
        android:textColor="#FFFFFF" />
</LinearLayout>

</FrameLayout>

这是一个错字,因为在tbAdd中应该是tbadd ...谢谢您的答复。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM