在我目前正在处理的项目中,我收到以下错误:
var colors = ['red','green','blue'];
...
strokeColor : colors[j]
这是我目前的代码
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
}
按钮的ID是正确的,所以我不确定为什么我遇到这个问题。我真的很感激任何帮助。
编辑:
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();
}
});
}
<?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" />
答案 0 :(得分:1)
您尚未在活动的主布局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" />
答案 1 :(得分:0)
你应该试试这个:
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;
...
}
}