[英]Android Studio Emulator Not Running. Codes are OK
我正在為我的公司編寫一個應用程序,但由於我的模擬器沒有運行,我看不到我的工作。
這是我的代碼如下。 沒有錯誤。 我在android studio中運行它。
它運行在一些應用程序上但我嘗試輸入我的代碼然后它不起作用。
請幫忙
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:關閉VM E / AndroidRuntime:FATAL EXCEPTION:main進程:com.example.tankoverfill,PID:10137 java.lang.RuntimeException:無法實例化活動ComponentInfo {com.example.tankoverfill / com.example.tankoverfill。 MainActivity}:java.lang.NullPointerException:嘗試在android.app.ActivityThread.performLaunchActivity(ActivityThread.java)的空對象引用上調用虛方法'android.view.Window $ Callback android.view.Window.getCallback()'。 2843)在android.app.servert事件中的android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)android.app.servertransaction.TransactionExecutor.executeCallbacks上的android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)(TransactionExecutor.java:108 )android.app.Handler.dispatchMessage(Handler.java:106)的android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1808)在android.os.Looper.lo op(Looper.java:193)位於android.app.ActivityThread.main(ActivityThread.java:6669),位於com.android.internal.os.RuntimeInit $ MethodAndArgsCaller的java.lang.reflect.Method.invoke(Native Method)。運行(RuntimeInit.java:493)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)引起:java.lang.NullPointerException:嘗試調用虛方法'android.view.Window $ Callback android .view.Window.getCallback()'在android.support.v7.app.AppCompatDelegateImpl。(AppCompatDelegateImpl.java:249)上的空對象引用,位於android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:182) )在android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)的android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)com.example.tankoverfill.MainActivity。(MainActivity) .java:15)位於android.support.v4.app.Cor的android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)的java.lang.Class.newInstance(Native Method) android.app.ActivityThread.handleLaunchActivity(ActivityThread)上android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)的android.app.Instrumentation.newActivity(Instrumentation.java:1215)上的eComponentFactory.instantiateActivity(CoreComponentFactory.java:43) .java:3048)在Android.app.ser.Tra.action.Te.proc.TravertactionExecutor.exe執行的android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)上的android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) TransactionExecutor.java:68)在Android.os.Looper.loop(Looper.java)的android.app.Handler.dispatchMessage(Handler.java:106)上的android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1808) :193)在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(ZygoteIni) t.java:858)I / Process:發送信號。 PID:10137 SIG:9應用程序終止。
您好代碼中的兩個錯誤是:
第一個錯誤,您必須將兩個textview鏈接到onCreate()方法內的xml
錯誤
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));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.