Coders_Lab
Jeżeli Twój kod wygląda tak samo jak w poście powyżej to w jaki sposób dodałeś nowe Activity (ComponentInfo)? Z logów które podałeś, wynika że ten adapter nie istnieje wComponentInfo.
A tak jakoś jaśniej się da? Dodałem nową klasę, napisałem ten kod, żeby były 2 spinnery i jakoś przeliczało... A potem podłączyłem pod MainActivity... i dopisałem do Mainfesta...
konwerter.java
package com.example.pracainzynierska;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class konwerter extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Spinner spinner = findViewById(R.id.Spinner1);
final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.jednostki, android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, final int position, long id) {
Button baton = findViewById(R.id.button);
baton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String actualposition = (String) spinner.getItemAtPosition(position) ;
EditText pole = findViewById(R.id.pole);
TextView widok = findViewById(R.id.textView3);
Float str = Float.valueOf(pole.getText().toString());
switch (position) {
case 0:
Toast.makeText(getApplicationContext(), "Pozycja pierwsza", Toast.LENGTH_SHORT).show();
String sum6 = String.format("%.2f", str);
widok.setText(sum6);
break;
case 1:
Toast.makeText(getApplicationContext(), "Pozycja druga", Toast.LENGTH_SHORT).show();
String sum7 = String.format("%.2f", str * 10);
widok.setText(sum7);
break;
case 2:
Toast.makeText(getApplicationContext(), "Pozycja trzecia", Toast.LENGTH_SHORT).show();
String sum8 = String.format("%.2f", str * 100);
widok.setText(sum8);
break;
case 3:
Toast.makeText(getApplicationContext(), "Pozycja czwarta", Toast.LENGTH_SHORT).show();
String sum9 = String.format("%.2f", str * 1000);
widok.setText(sum9);
break;
case 4:
Toast.makeText(getApplicationContext(), "Pozycja piata", Toast.LENGTH_SHORT).show();
String sum10 = String.format("%.2f", str * 10000);
widok.setText(sum10);
break;
}
}
});
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Spinner spinner2 = findViewById(R.id.Spinner2);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.jednostki, android.R.layout.simple_spinner_dropdown_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, final int position, long id) {
Button baton = findViewById(R.id.button);
baton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText pole = findViewById(R.id.pole);
TextView widok = findViewById(R.id.textView3);
Float str = Float.valueOf(pole.getText().toString());
switch (position) {
case 0:
Toast.makeText(getApplicationContext(), "Pozycja pierwsz", Toast.LENGTH_SHORT).show();
String sum = String.format("%.2f", str);
widok.setText(sum);
break;
case 1:
Toast.makeText(getApplicationContext(), "Pozycja drug", Toast.LENGTH_SHORT).show();
String sum1 = String.format("%.2f", str / 10);
widok.setText(sum1);
break;
case 2:
Toast.makeText(getApplicationContext(), "Pozycja trzeia", Toast.LENGTH_SHORT).show();
String sum3 = String.format("%.2f", str / 100);
widok.setText(sum3);
break;
case 3:
Toast.makeText(getApplicationContext(), "Pozycja czwata", Toast.LENGTH_SHORT).show();
String sum4 = String.format("%.3f", str / 1000);
widok.setText(sum4);
break;
case 4:
Toast.makeText(getApplicationContext(), "Pozycja pata", Toast.LENGTH_SHORT).show();
String sum5 = String.format("%.4f", str / 10000);
widok.setText(sum5);
break;
}
}
});
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
kon.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"
tools:context=".MainActivity">
<Spinner
android:id="@+id/Spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginTop="150dp" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="100dp"
android:text="Podaj wartość: " />
<EditText
android:id="@+id/pole"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="130dp"
android:layout_marginTop="80dp"
android:inputType="number"
android:text="" />
<Spinner
android:id="@+id/Spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="210dp"
android:layout_marginTop="150dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="380dp"
android:layout_marginLeft="160dp"
android:text="Przelicz" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wynik"
android:layout_marginTop="300dp"
android:layout_marginLeft="100dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView3"
android:text=" "
android:layout_marginTop="300dp"
android:layout_marginLeft="180dp"/>
</RelativeLayout>
I mainActivity.java
package com.example.pracainzynierska;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button konwerter;
Button przeliczanietemp;
Button kontakt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
konwerter = (Button) findViewById(R.id.button2);
konwerter.setOnClickListener(this);
przeliczanietemp = (Button) findViewById(R.id.przycisk_temperatura);
przeliczanietemp.setOnClickListener(this);
kontakt = (Button) findViewById(R.id.button4);
kontakt.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId() ) {
case R.id.przycisk_temperatura:
Intent intent = new Intent(this, przeliczanietemp.class);
startActivity(intent);
break;
case R.id.button2:
Intent cos = new Intent(this, konwerter.class);
startActivity(cos);
break;
case R.id.button4:
Intent kontakt = new Intent(this, kontakt.class);
startActivity(kontakt);
default:
break;
}
}
}
Edit... Już wiem co było źle...
setContentView(R.layout.activity_main);
Ta linijka iż gdyż ja mam innego layouta, a pobierało mi z Activity_main... Tak czy tak... Dzięki za zainteresowanie 😉
A tak co do tego ListView, żeby tam pokazywały się rzeczy gdy wybiorę inny RadioButton... To jak to zrobić... W sensie chodzi mi o to "wysyłanie"...?
Bo jak ja miałbym robić to przechodzi to do innego Activity i 2 Buttony 😉 Dla mnie to najłatwiejszy sposób, ale ciekawość i potrzeba 😉