Я пишу приложение для своей компании, но не вижу свою работу, так как мой эмулятор не запущен.
Вот мой код ниже. Ошибок нет. Я запускаю это в студии Android.
Он работает в некоторых приложениях, но я попытался ввести свой код, но он не работает.
Пожалуйста помоги
package com.example.tankoverfill;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button calc;
TextView tv_heightfills = findViewById(R.id.tv_heightfills);
TextView tv_timefills = findViewById(R.id.tv_timefills);
EditText flow, areas, heights, densities, heightones, cvs;
int x1,y1,w1,a1, x2,y2,w2,a2,g;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flow = findViewById(R.id.flow);
areas = findViewById(R.id.areas);
heights = findViewById(R.id.heights);
densities = findViewById(R.id.densities);
heightones = findViewById(R.id.heightones);
cvs = findViewById(R.id.cvs);
calc = findViewById(R.id.calc);
calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
x1 = Integer.parseInt(flow.getText().toString());
y1 = Integer.parseInt(cvs.getText().toString());
w1 = Integer.parseInt(densities.getText().toString());
g = (int) 9.8;
a1 = (x1 / (y1*w1*g));
tv_heightfills.setText(a1);
}
});
calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
x2 = Integer.parseInt(areas.getText().toString());
y2 = Integer.parseInt(cvs.getText().toString());
w2 = Integer.parseInt(densities.getText().toString());
g = (int) 9.8;
a2 = (x1 / (y1*w1*g));
tv_timefills.setText(a2);
}
});
}
}
D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.tankoverfill, PID: 10137 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.tankoverfill/com.example.tankoverfill.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 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:1808) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.support.v7.app.AppCompatDelegateImpl.(AppCompatDelegateImpl.java:249) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:182) at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520) at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:191) at com.example.tankoverfill.MainActivity.(MainActivity.java:15) at java.lang.Class.newInstance(Native Method) at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69) at android.support.v4.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43) at android.app.Instrumentation.newActivity(Instrumentation.java:1215) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 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:1808) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) I/Process: Sending signal. PID: 10137 SIG: 9 Application terminated.
Здравствуйте, две ошибки в вашем коде:
первая ошибка, которую вы должны связать два текстовых представления с xml внутри метода onCreate()
ошибка
TextView tv_heightfills = findViewById(R.id.tv_heightfills);
TextView tv_timefills = findViewById(R.id.tv_timefills);
правильный
TextView tv_heightfills;
TextView tv_timefills;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_heightfills = findViewById(R.id.tv_heightfills);
tv_timefills = findViewById(R.id.tv_timefills);
....
}
Вторая ошибка - установка значения int для метода textview setText()
так и должно быть:
tv_heightfills.setText(String.valueOf(a1));
то же самое для
tv_timefills.setText(String.valueOf(a2));
Вау, спасибо большое! Получил работу в приложении. Ты гений! Нужно только теперь настроить ограничения.