在导航抽屉片段中设置onClickListener会导致应用程序崩溃

时间:2016-02-16 06:56:59

标签: android android-fragments android-studio

当我将onclick监听器添加到导航抽屉片段时应用程序崩溃,但是当我删除它时一切正常:

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显示此NullPointException:

  

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

更新: 抽屉CLass:     包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文件:     

<!-- 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>

1 个答案:

答案 0 :(得分:0)

这是一个错字,因为在tbAdd应该是tbadd ... 谢谢你的回复。