Quando vou executar ele diz que o app parou de funcionar.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("aula3db");
ItemEstoque item = new ItemEstoque("1", "caneta", "10");
//Armazenando o objeto item em uma estrutura "item_estoque":
myRef.child("item_estoque").child(item.itemId).setValue(item);
ItemEstoque item2 = new ItemEstoque("2", "lapis", "20");
//Armazenando o objeto item em uma estrutura "item_estoque":
myRef.child("item_estoque").child(item.itemId).setValue(item2);
//Leitura dos dados armazenados em "item_estoque":
Query query = myRef.child("item_estoque").orderByChild("descricao").orderByValue();
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//exibe na tela os valores inseridos na tabela
for ( DataSnapshot linha : dataSnapshot.getChildren())
{
Toast.makeText(MainActivity.this,
"Valor inserido: " + linha.child("descricao"),
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError error) {
Toast.makeText(MainActivity.this,
"Falha na leitura de dados: " + error.toException(),
Toast.LENGTH_SHORT).show();
}
});
}
}
classe ItemEstoque
public class ItemEstoque {
public String itemId;
public String descricao;
public String quantidade;
public ItemEstoque() {
}
public ItemEstoque(String itemId, String descricao, String quantidade) {
this.itemId = itemId;
this.descricao = descricao;
this.quantidade = quantidade;
}
}
classe SQLite
public class MeuSQLite extends SQLiteOpenHelper {
public MeuSQLite(Context context, String database_name, int i)
{
super(context, database_name, null, 1);
}
/* Método para criação das tabelas do banco de dados */
public void onCreate(SQLiteDatabase db)
{
db.execSQL("Create table item " +
" (id integer primary key autoincrement, descricao text, quantidade integer);" );
}
/* Método para controle de mudanças na estrutura do banco de dados */
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity"
android:background = "#ffffff"
android:orientation = "vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ITEM EM ESTOQUE:"
android:textColor="@color/colorPrimary">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nome do item:">
</TextView>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/item"></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quantidade do item:">
</TextView>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/quantidade"></EditText>
<Button
android:id = "@+id/enviar"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_gravity = "center"
android:text = "Enviar"></Button>
<Button
android:id = "@+id/listar"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_gravity = "center"
android:text = "Listar"></Button>
<Button
android:id = "@+id/duplicar_quantidade"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_gravity = "center"
android:text = "Duplicar quantidades"></Button>
</LinearLayout>
erro:
2021-04-11 19:06:40.042 5762-5762/? E/memtrack: Couldn’t load memtrack module (No such file or directory)
2021-04-11 19:06:40.042 5762-5762/? E/android.os.Debug: failed to load memtrack module: -2
2021-04-11 19:06:40.222 5773-5773/com.example.mysqlite E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mysqlite, PID: 5773
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mysqlite/com.example.mysqlite.MainActivity}: java.lang.IllegalArgumentException: You can’t combine multiple orderBy calls!
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.IllegalArgumentException: You can’t combine multiple orderBy calls!
at com.google.firebase.database.Query.validateNoOrderByCall(Query.java:137)
at com.google.firebase.database.Query.orderByValue(Query.java:869)
at com.example.mysqlite.MainActivity.onCreate(MainActivity.java:38)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)