在Android Studio中插入数值时遇到的问题。

huangapple go评论69阅读模式
英文:

Troubles with Inserting values. Android Studio

问题

以下是翻译好的内容:

SecondPan.java:

public class SecondPan extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private Spinner sspnOption6, sspn2;

    EditText umowa, nazwa, kategorie, opis, zabezpieczenia, dane;
    String czy_dane, transfer;
    private Button btn_insert;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_pan);

        Spinner sspn2;
        Spinner sspnOption6;

        umowa = (EditText) findViewById(R.id.scnd_txt1);
        nazwa = (EditText) findViewById(R.id.scnd_txt2);
        kategorie = (EditText) findViewById(R.id.scnd_txt3);
        opis = (EditText) findViewById(R.id.scnd_tx4);
        zabezpieczenia = (EditText) findViewById(R.id.scnd_tx6);
        dane = (EditText) findViewById(R.id.scnd_tx7);

        sspnOption6 = findViewById(R.id.sspn);
        ArrayAdapter<CharSequence> FirstAdapter = new ArrayAdapter<CharSequence>(
            this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.tab1));
        FirstAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sspnOption6.setAdapter(FirstAdapter);

        sspn2 = findViewById(R.id.sspn2);
        ArrayAdapter<CharSequence> Secondadapter = new ArrayAdapter<CharSequence>(
            this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.transfer));
        Secondadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sspn2.setAdapter(Secondadapter);
        czy_dane = sspn2.getSelectedItem().toString();
        transfer = sspnOption6.getSelectedItem().toString();
    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
        String text = parent.getItemAtPosition(position).toString();
        Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
    }

    public void OnInsert(View view) {
        String str_czy_dane = sspn2.getSelectedItem().toString();
        String str_umowa = umowa.getText().toString();
        String str_nazwa = nazwa.getText().toString();
        String str_kategorie = kategorie.getText().toString();
        String str_opis = opis.getText().toString();
        String str_transfer = sspnOption6.getSelectedItem().toString();
        String str_zabezpieczenia = zabezpieczenia.getText().toString();
        String str_dane = dane.getText().toString();
        String type = "insert";
        BackgroundWorker backgroundWorker = new BackgroundWorker(this);
        backgroundWorker.execute(type, str_czy_dane, str_umowa, str_nazwa, str_kategorie, str_opis,
            str_transfer, str_zabezpieczenia, str_dane);
    }
}

BackgroundWorker.java:

public class BackgroundWorker extends AsyncTask<String, Void, String> {
    Context context;
    AlertDialog alertDialog;

    BackgroundWorker(Context ctx) {
        context = ctx;
    }

    @Override
    protected String doInBackground(String... params) {
        // ... (在这里省略了 doInBackground 的内容)
    }

    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Login status");
    }

    @Override
    protected void onPostExecute(String result) {
        if (result.equals("User login success")) {
            Intent intent = new Intent(context, SecondPan.class);
            context.startActivity(intent);
        } else if (result.equals("Admin login success")) {
            Intent intent2 = new Intent(context, AdminPanel.class);
            context.startActivity(intent2);
        } else {
            alertDialog.setMessage(result);
            alertDialog.show();
        }
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
}
英文:

I have issues with inserting values to my database. I am still looking for a problem, but I am helpless. When I press button "insert" app stopps and closes. Php script is ok. I have no idea where is problem so I post two class there. Thanks for information.

SecondPan.java:

public class SecondPan extends AppCompatActivity implements AdapterView.OnItemClickListener {
private Spinner sspnOption6, sspn2;
EditText umowa,nazwa,kategorie,opis,zabezpieczenia,dane;
String czy_dane, transfer;

private Button btn_insert;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_pan);
Spinner sspn2 ;
Spinner sspnOption6 ;
umowa = (EditText) findViewById(R.id.scnd_txt1);
nazwa = (EditText) findViewById(R.id.scnd_txt2);
kategorie = (EditText) findViewById(R.id.scnd_txt3);
opis = (EditText) findViewById(R.id.scnd_tx4);
zabezpieczenia = (EditText) findViewById(R.id.scnd_tx6);
dane = (EditText) findViewById(R.id.scnd_tx7);
sspnOption6 = findViewById(R.id.sspn);
ArrayAdapter<CharSequence> FirstAdapter = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.tab1));
FirstAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sspnOption6.setAdapter(FirstAdapter);
sspn2=findViewById(R.id.sspn2);
ArrayAdapter<CharSequence> Secondadapter = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.transfer));
Secondadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sspn2.setAdapter(Secondadapter);
czy_dane = sspn2.getSelectedItem().toString();
transfer = sspnOption6.getSelectedItem().toString();
}
public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
}
public void OnInsert(View view ) {
String str_czy_dane = sspn2.getSelectedItem().toString();
String str_umowa = umowa.getText().toString();
String str_nazwa = nazwa.getText().toString();
String str_kategorie = kategorie.getText().toString();
String str_opis = opis.getText().toString();
String str_transfer  = sspnOption6.getSelectedItem().toString();
String str_zabezpieczenia = zabezpieczenia.getText().toString();
String str_dane = dane.getText().toString();
String type = "insert";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, str_czy_dane,str_umowa, str_nazwa, str_kategorie, str_opis,str_transfer, str_zabezpieczenia, str_dane);
}

}

BackgroundWorker.java:

public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx) {
context = ctx;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://10.0.2.2/login.php";
String register_url="http://10.0.2.2/register.php";
String insert_url = "http://10.0.2.2/wspol_adm.php";
String login_admin_url= "http://10.0.2.2/login_adm.php";
String admin_url="http://10.0.2.2/admin.php";

if(type.equals("insert")){

        try {
String czy_dane = params[1];
String umowa = params[2];
String nazwa = params[3];
String kategorie = params[4];
String opis = params[5];
String transfer = params[6];
String zabezpieczenia = params[7];
String dane = params[8];
URL url = new URL (insert_url);
HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String post_data = URLEncoder.encode("czy_dane","UTF-8")+"="+URLEncoder.encode(czy_dane,"UTF-8")+"&"+
URLEncoder.encode("umowa","UTF-8")+"="+URLEncoder.encode(umowa,"UTF-8")+"&"
+URLEncoder.encode("nazwa","UTF-8")+"="+URLEncoder.encode(nazwa,"UTF-8")+"&"
+URLEncoder.encode("kategorie","UTF-8")+"="+URLEncoder.encode(kategorie,"UTF-8")+"&"
+URLEncoder.encode("opis","UTF-8")+"="+URLEncoder.encode(opis,"UTF-8")+"&"
+URLEncoder.encode("transfer","UTF-8")+"="+URLEncoder.encode(transfer,"UTF-8")+"&"
+URLEncoder.encode("zabezpieczenia","UTF-8")+"="+URLEncoder.encode(zabezpieczenia,"UTF-8")+"&"
+URLEncoder.encode("dane","UTF-8")+"="+URLEncoder.encode(dane,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1")) ;
String result="";
String line="";
while((line = bufferedReader.readLine())!=null) {
result+= line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login status");
}
@Override
protected void onPostExecute(String result) {
if(result.equals("User login success")) {
Intent intent = new Intent(context, SecondPan.class);
context.startActivity(intent);
}
if(result.equals("Admin login success")){
Intent intent2 = new Intent(context, AdminPanel.class);
context.startActivity(intent2);
}
else {
alertDialog.setMessage(result);
alertDialog.show();
}
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}

}

答案1

得分: 1

尝试一下:

SecondPan.java类的onCreate()方法中删除以下代码行:

Spinner sspn2;
Spinner sspnOption6;
英文:

Try this:

Remove the following lines of code from SecondPan.java class inside onCreate()

Spinner sspn2 ;
Spinner sspnOption6 ;

huangapple
  • 本文由 发表于 2020年9月26日 21:30:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/64078240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定