我試圖做一個android應用程序,需要一個輸入的十進制數並將其轉換爲二進制代碼。現在,每當我通過模擬器運行應用程序時,我都會收到以下編譯錯誤:編輯:更多的日誌信息和代碼更改,仍然存在相同的問題。NullPointException,二進制計算器應用程序
07-25 19:46:35.814: E/AndroidRuntime(2248): FATAL EXCEPTION: main
07-25 19:46:35.814: E/AndroidRuntime(2248): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.binaryconverter/com.example.binaryconverter.MainActivity}: java.lang.NullPointerException
07-25 19:46:35.814: E/AndroidRuntime(2248): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
07-25 19:46:35.814: E/AndroidRuntime(2248): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-25 19:46:35.814: E/AndroidRuntime(2248): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-25 19:46:35.814: E/AndroidRuntime(2248): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-25 19:46:35.814: E/AndroidRuntime(2248): at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 19:46:35.814: E/AndroidRuntime(2248): at android.os.Looper.loop(Looper.java:137)
07-25 19:46:35.814: E/AndroidRuntime(2248): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-25 19:46:35.814: E/AndroidRuntime(2248): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 19:46:35.814: E/AndroidRuntime(2248): at java.lang.reflect.Method.invoke(Method.java:511)
07-25 19:46:35.814: E/AndroidRuntime(2248): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-25 19:46:35.814: E/AndroidRuntime(2248): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-25 19:46:35.814: E/AndroidRuntime(2248): at dalvik.system.NativeStart.main(Native Method)
07-25 19:46:35.814: E/AndroidRuntime(2248): Caused by: java.lang.NullPointerException
07-25 19:46:35.814: E/AndroidRuntime(2248): at android.app.Activity.findViewById(Activity.java:1839)
07-25 19:46:35.814: E/AndroidRuntime(2248): at com.example.binaryconverter.MainActivity.<init>(MainActivity.java:30)
07-25 19:46:35.814: E/AndroidRuntime(2248): at java.lang.Class.newInstanceImpl(Native Method)
07-25 19:46:35.814: E/AndroidRuntime(2248): at java.lang.Class.newInstance(Class.java:1319)
07-25 19:46:35.814: E/AndroidRuntime(2248): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
07-25 19:46:35.814: E/AndroidRuntime(2248): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
這是一個班級,我很難找出解決方案。
package com.example.binaryconverter;
import android.renderscript.Script.FieldID;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.*;
import android.widget.*;
import android.view.View.OnClickListener;
import java.io.*;
import java.util.*;
import java.math.*;
public class MainActivity extends Activity implements OnClickListener{
int inputNumber = 0; //inputs the number for the conversion to take place
int i=0; //integer to divide by
int number = 0; //number given by field
public String toBinary(int decimal)
{
String result = "" ;
while (decimal > 0) {
int bit = decimal % 2 ;
result = "" + bit + result ;
decimal = decimal/2 ;
}
for (int i=result.length(); i <= 8; i++) {
result = "0" + result ;
}
return result ;
}
Button calc = (Button)findViewById(R.id.convert);
Button clear = (Button)findViewById(R.id.clear);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calc.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
TextView display = (TextView)findViewById(R.id.binaryView);
EditText input = (EditText)findViewById(R.id.inputField);
int inputs = Integer.parseInt(input.getText().toString());
calc = (Button) v ;
if (v.getId() == clear.getId()) {
display.setText("") ;
input.setText("") ;
} else if (v.getId() == calc.getId()) {
if (input.getText() != null) {
int value = Integer.parseInt(input.getText().toString()) ;
display.setText(toBinary(value)) ;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
可能是輸入實例變量爲null。 – rozar
您不能在onCreate()之前運行的代碼中使用findViewById(),例如方法外部的代碼。按照已經指示的方式將這些調用移入onCreate()中。 –