如何在Firebase fatabase中创建一个数据库,该数据库使用此代码获取随机数据。我创建了数据库,但是我的应用程序崩溃了。
LogCat在行上显示错误:
myInteger = Integer.parseInt((字符串)
dataSnapshot.child(“ the_integer”)。getValue());
我确定是因为我的数据库错误。
我的数据库结构是:
import React from 'react';
import PivotTableUI from 'react-pivottable/PivotTableUI';
import 'react-pivottable/pivottable.css';
import TableRenderers from 'react-pivottable/TableRenderers';
import Plot from 'react-plotly.js';
import createPlotlyRenderers from 'react-pivottable/PlotlyRenderers';
// create Plotly renderers via dependency injection
const PlotlyRenderers = createPlotlyRenderers(Plot);
const data1 = [{'Country':'USA','Sales':45000},
{'Country':'USA','Sales':50000},{'Country':'CA','Sales':15000}]
const data2 = [{'Product':'Sofa','Sales':5000},{'Product':'Dinner
Table','Sales':50000},{'Product':'Chair','Sales':15000}]
const dataDic = {'Region':data1,'Products':data2}
class App extends React.Component {
constructor(props) {
super(props);
this.state = {selectedOption: 'Region'};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({selectedOption: event.target.value});
}
handleSubmit(event) {
alert('You have selected ' + this.state.selectedOption);
event.preventDefault();
}
render() {
return <div>
<select defaultValue="Region" onChange={(e)=>
this.handleChange(e)}>
<option value="Region">Region</option>
<option value="Products">Products</option>
</select>
<br/>
<br/>
<PivotTableUI
data={dataDic[this.state.selectedOption]}
onChange={s => this.setState(s)}
renderers={Object.assign({},TableRenderers)}//,PlotlyRenderers)}
{...this.state}
/>
</div>;
}
}
export default App;
我的服务活动是:
databaseRefrence
|
|_______"data"
| |
| |_______"people"
| |
| |_______"cities"
|
|_______"count"
|
|_______"the_integer"
|
|_______"my_order_type"
}
LogCat错误:
public class MyService {
private Variables;
private Random random;
Context context;
DatabaseRefrence refrence;
RelativeLayout myLayout;
DatabaseRefrence database;
DatabaseRefrence refrenceSize;
int myInteger;
Button btnLoad;
MyService(Context context, RelativeLayout myLayout, Button btnLoad) {
this.context = context;
this.myLayout = myLayout;
this.btnLoad = btnLoad;
this.database = FirebaseDatabase.getInstance().getReference().child("data");
this.refrenceSize = FirebaseDatabase.getInstance().getReference().child("count");
this.random = new Random();
}
public void showRandom() {
refrenceSize.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
myInteger = Integer.parseInt((String) dataSnapshot.child("the_integer").getValue());
refrence = database.child(String.valueOf(random.nextInt(myInteger)));
refrence.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {
String order = (String) dataSnapshot.child("my_order_type").getValue();
btnLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (order.equals(people)) {
//Do something
} else if (order.equals(cities)) {
//Do something
}
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
//Error
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
//Error
}
});
}