gpt4 book ai didi

java - 空指针异常 - Android 分配 setOnClickListener

转载 作者:行者123 更新时间:2023-12-01 10:09:41 25 4
gpt4 key购买 nike

在我当前正在处理的项目中,我收到以下错误:

 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>

最佳答案

您尚未在 Activity 的主布局 xml 中使用第二个 xml content_lunch_home。这就是为什么您无法找到 content_lunch_home 中声明的按钮。

这样做:

<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" />

关于java - 空指针异常 - Android 分配 setOnClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36210473/

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