簡體   English   中英

空指針異常-Android分配setOnClickListener

[英]Null Pointer Exception - Android Assign setOnClickListener

在我當前正在從事的項目中,我收到以下錯誤:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{midamcorp.com.burgerkingapp/midamcorp.com.burgerkingapp.lunchHome}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object 

這是我目前的代碼

 public class lunchHome extends AppCompatActivity {
 private SimpleCursorAdapter adapter;

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

    databaseHelper dbHelper = new databaseHelper(this);
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    SharedPreferences preferences = getSharedPreferences("config", MODE_PRIVATE);
    if(preferences.getInt("Location", 2) == 3) {
        discriminator = 3;
    } else if(preferences.getInt("Location", 2) == 2){
        discriminator = 2;
    }

    Button ltoButton = (Button)findViewById(R.id.lunchLTOButton);
    Button standardButton = (Button)findViewById(R.id.lunchMainButton);

    standardButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(lunchHome.this, lunchStandard.class);
            startActivity(intent);

        }
    });

    ltoButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(lunchHome.this, lunchLTOs.class);
            startActivity(intent);
        }
    });
 Cursor results = db.rawQuery("SELECT _id, name, icon, buildImage FROM " + databaseHelper.TABLE_NAME + " WHERE location = " + discriminator + " and LTO = " + 1 + " LIMIT 3", null);

 String[] from = new String [] {databaseHelper.ITEM_NAME};
 int[] to = new int[] {R.id.newName};

    adapter = new SimpleCursorAdapter(this, R.layout.item_list, results, from, to, 0);
    final ListView listContainer = (ListView) findViewById(R.id.listContainer);
    listContainer.setAdapter(adapter);

    listContainer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Cursor cursor = (Cursor) listContainer.getItemAtPosition(position);
            int imageId = cursor.getInt(cursor.getColumnIndex("buildImage"));
 Intent intent = new Intent(lunchHome.this,        midamcorp.com.burgerkingapp.buildImage.class);
            intent.putExtra("imageID", imageId );
            startActivity(intent);

        }
    });


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

}

按鈕的ID是正確的,所以我不確定為什么會有這個問題。 我非常感謝您的幫助。

編輯:

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

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>



<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

     <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="midamcorp.com.burgerkingapp.lunchHome">

<TextView android:text="LTO's" android:textSize="48dp" android:textAlignment="center" android:layout_marginBottom="50dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<ListView android:id="@+id/listContainer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
   <Button android:text="View All" android:id="@+id/lunchLTOButton"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />
   <TextView android:text="Main Items" android:textSize="48dp" android:textAlignment="center" android:layout_marginBottom="50dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

  <Button android:text="View" android:id="@+id/lunchMainButton"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />


</LinearLayout>

您尚未在活動的主布局xml中使用第二個xml content_lunch_home 因此,您無法找到content_lunch_home聲明的Button。

做這樣的事情:

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay" />

<include
android:id="@+id/content_lunch_home"  
android:layout_height="wrap_content"
android:layout_width="match_parent" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />

您應該嘗試這樣:

public class MyActivity extends AppCompatActivity implements View.OnClickListener {

   Button buttona, buttonb, buttonc;

   @Override
   public void onCreate(Bundle bundle) {
       super.onCreate();

       ...

       buttona = (Button) findViewById(R.id.buttona);
       buttonb = (Button) findViewById(R.id.buttonb);
       buttonc = (Button) findViewById(R.id.buttonc);

       buttona.setOnClickListener(this);
       buttonb.setOnClickListener(this);
       buttonc.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
      switch(v.getId()) {
          case R.id.buttona:
          // do something;
          break;
          case R.id.buttonb:
          // do something;
          break;
      ...
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM