每次我尝试开公司时,我的 apa 都会崩溃 我希望应用程序发送输入文本中存在的数据 通过 url 能够通过 php 脚本获取它们 你有什么解决办法吗? 我已经尝试了几次更改,但目前没有任何效果 我不明白错误在哪里,有什么帮助吗?
这可能是一个简单的愚蠢错误,但我找不到。我附上了 java、xml 文件和 logcat。
java代码:
String nomep;
int prezzop, tempop;
Button btnInserisci;
RadioButton aggiorna;
RadioButton servizio;
RadioButton ricambio;
RadioButton accessori;
EditText nome;
EditText prezzo;
EditText tempo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inserisci);
aggiorna = findViewById(R.id.aggiorna);
servizio = findViewById(R.id.servizio);
ricambio = findViewById(R.id.ricambio);
accessori = findViewById(R.id.acessori);
nome = findViewById(R.id.nomeProdotto);
prezzo = findViewById(R.id.prezzo);
tempo = findViewById(R.id.tempo);
//pulsante in ascolto del "click"
btnInserisci = findViewById(R.id.scanInserisci);
btnInserisci.setOnClickListener(this);
scan();
}
//se il pulsante viene cliccato apre la funzione "scan"
@Override
public void onClick(View v) {
scan();
}
private void scan(){
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setCaptureActivity(CaptureAct.class);
integrator.setOrientationLocked(false);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scannerizzando il codice..");
integrator.initiateScan();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null){
if(result.getContents() != null){
if(aggiorna.isChecked()) {
if(accessori.isChecked()){
//stringa sito da cambiare con quella del server funzionante
String sito = "https://www.gorinsights.it/scuola/server.php?nuovo=no&aggiungi=si&tabella=accessori&id=" + result.getContents();
gotoUrl(sito);
}else if (ricambio.isChecked()){
//stringa sito da cambiare con quella del server funzionante
String sito = "https://www.gorinsights.it/scuola/server.php?nuovo=no&aggiungi=si&tabella=ricambio&id=" + result.getContents();
gotoUrl(sito);
}else if (servizio.isChecked()){
Toast.makeText(this,"non puoi aggiungere quantità ad un servizio", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this,"scegli dove aggiornare il prodotto", Toast.LENGTH_LONG).show();
}
}else{
if(accessori.isChecked()){
nomep = nome.getText().toString();
prezzop = Integer.valueOf(prezzo.getText().toString());
//stringa da aggiustare
String sito = "https://www.gorinsights.it/scuola/server.php?nuovo=si&tabella=accessori&id=" + result.getContents()+"&nome="+nomep+"&prezzo="+prezzop;
gotoUrl(sito);
}else if (ricambio.isChecked()){
nomep = nome.getText().toString();
prezzop = Integer.valueOf(prezzo.getText().toString());
//stringa da aggiustare
String sito = "https://www.gorinsights.it/scuola/server.php?nuovo=si&tabella=ricambio&id=" + result.getContents()+"&nome="+nomep+"&prezzo="+prezzop;
gotoUrl(sito);
}else if (servizio.isChecked()){
nomep = nome.getText().toString();
prezzop = Integer.valueOf(prezzo.getText().toString());
tempop=Integer.valueOf(tempo.getText().toString());
//stringa da aggiustare
String sito = "https://www.gorinsights.it/scuola/server.php?nuovo=si&tabella=servizio&id=" + result.getContents()+"&nome="+nomep+"&prezzo="+prezzop+"&tempo="+tempop;
gotoUrl(sito);
}else{
Toast.makeText(this,"scegli dove inserire il prodotto", Toast.LENGTH_LONG).show();
}
}
}
else
{
Toast.makeText(this,"nessun risultato", Toast.LENGTH_LONG).show();
}
}else {
super.onActivityResult(requestCode, resultCode, data);
}
}
private void gotoUrl(String sito) {
Uri uri = Uri.parse(sito);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
// Verify that the intent will resolve to an activity
if (intent.resolveActivity(getPackageManager()) != null) {
// Here we use an intent without a Chooser unlike the next example
startActivity(intent);
}
}
xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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:padding="20dp"
tools:context=".inserisci">
<Button
android:id="@+id/scanInserisci"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scan"
android:translationX="400px"
android:translationY="1500px" />
<RadioButton
android:id="@+id/aggiungi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Aggiungi nuovo"
android:translationX="100px"
android:translationY="200px" />
<RadioButton
android:id="@+id/servizio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="servizio"
android:translationX="750px"
android:translationY="350px" />
<RadioButton
android:id="@+id/ricambio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ricambio"
android:translationX="400px"
android:translationY="350px" />
<RadioButton
android:id="@+id/acessori"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="acessori"
android:translationX="80px"
android:translationY="350px" />
<RadioButton
android:id="@+id/aggiorna"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Aggiorna quantià"
android:translationX="600px"
android:translationY="200px" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:translationY="500px"
android:id="@+id/nomeProdotto">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="nome prodotto" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:translationY="700px"
android:id="@+id/prezzo"
>
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="prezzo" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:translationY="1000px"
android:id="@+id/tempo"
>
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="tempo necessario (obbligatorio solo per i servizi)" />
</com.google.android.material.textfield.TextInputLayout>
崩溃日志:
2021-01-26 13:46:47.709 8672-8672/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.scanner, PID: 8672
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.scanner/com.example.scanner.inserisci}: android.view.InflateException: Binary XML file line #64: com.google.android.material.textfield.TextInputEditText cannot be cast to android.view.ViewGroup
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6810)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: android.view.InflateException: Binary XML file line #64: com.google.android.material.textfield.TextInputEditText cannot be cast to android.view.ViewGroup
Caused by: java.lang.ClassCastException: com.google.android.material.textfield.TextInputEditText cannot be cast to android.view.ViewGroup
at android.view.LayoutInflater.rInflate(LayoutInflater.java:868)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:870)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.inflate(LayoutInflater.java:519)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.scanner.inserisci.onCreate(inserisci.java:36)
at android.app.Activity.performCreate(Activity.java:7224)
at android.app.Activity.performCreate(Activity.java:7213)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2926)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6810)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
答案 0 :(得分:4)
您的错误消息说明了一切:
<块引用>引起:android.view.InflateException:二进制 XML 文件第 64 行:com.google.android.material.textfield。TextInputEditText 无法转换为 android.view.ViewGroup
您正在尝试将 <com.google.android.material.textfield.TextInputLayout
转换为 EditText,它们不是一回事:
在 onCreate 你有:
nome = findViewById(R.id.nomeProdotto);
您已将其定义为
EditText nome; <-- this is defined in your activity
但是在你的 xml 中你有
<com.google.android.material.textfield.TextInputLayout <-- this is defined in your xml
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:translationY="500px"
android:id="@+id/nomeProdotto">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="nome prodotto" />
</com.google.android.material.textfield.TextInputLayout>
你可能想要做的事情是这样的:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:translationY="500px">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/nomeProdotto"
android:hint="nome prodotto" />
</com.google.android.material.textfield.TextInputLayout>
答案 1 :(得分:2)
更新了您的 xml 代码。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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:padding="20dp"
tools:context=".inserisci">
<Button
android:id="@+id/scanInserisci"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scan"
android:translationX="400px"
android:translationY="1500px" />
<RadioButton
android:id="@+id/aggiungi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Aggiungi nuovo"
android:translationX="100px"
android:translationY="200px" />
<RadioButton
android:id="@+id/servizio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="servizio"
android:translationX="750px"
android:translationY="350px" />
<RadioButton
android:id="@+id/ricambio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ricambio"
android:translationX="400px"
android:translationY="350px" />
<RadioButton
android:id="@+id/acessori"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="acessori"
android:translationX="80px"
android:translationY="350px" />
<RadioButton
android:id="@+id/aggiorna"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Aggiorna quantià"
android:translationX="600px"
android:translationY="200px" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:translationY="500px">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/nomeProdotto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="nome prodotto" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:translationY="700px">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/prezzo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="prezzo" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:translationY="1000px">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tempo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="tempo necessario (obbligatorio solo per i servizi)" />
</com.google.android.material.textfield.TextInputLayout>