0

我尝试在android中制作从字符串到二进制的转换器

这是我的主要活动

public class MainActivity extends Activity {
    Converter converter;
    static EditText str;
    TextView hasil;
    Button convert;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    str = (EditText)findViewById(R.id.et_string);
    hasil = (TextView)findViewById(R.id.txt_hasil);

    final String kata =str.toString();
    convert = (Button)findViewById(R.id.btn_conv);
    convert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            converter = new Converter();
            converter.aksi1();
        }
    });
}

@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;
}

}

这是我的转换器类

public class Converter extends MainActivity {
    EditText str;
    TextView hasil;
    String kata = str.getText().toString();
    public void aksi1(){
            if (kata != null){

            byte[] bytes = kata.getBytes();
            StringBuilder binary = new StringBuilder();
            for(byte b : bytes){
                int val = b;
                for (int i = 0; i < 8; i++){
                    binary.append((val & 128) == 0 ? 0 : 1);// i dont know what this mean
                    val <<= 1;
                }
            }
            hasil.setText(binary);
        }
        else{
            convert.setEnabled(false);
        }


}

}

这是我运行该代码时遇到的错误

07-30 15:17:39.798: E/AndroidRuntime(495): FATAL EXCEPTION: main
07-30 15:17:39.798: E/AndroidRuntime(495): java.lang.NullPointerException
07-30 15:17:39.798: E/AndroidRuntime(495):  at com.example.strtobin.Converter.<init>(Converter.java:11)
07-30 15:17:39.798: E/AndroidRuntime(495):  at com.example.strtobin.MainActivity$1.onClick(MainActivity.java:34)
07-30 15:17:39.798: E/AndroidRuntime(495):  at android.view.View.performClick(View.java:2408)
07-30 15:17:39.798: E/AndroidRuntime(495):  at android.view.View$PerformClick.run(View.java:8816)
07-30 15:17:39.798: E/AndroidRuntime(495):  at android.os.Handler.handleCallback(Handler.java:587)
07-30 15:17:39.798: E/AndroidRuntime(495):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-30 15:17:39.798: E/AndroidRuntime(495):  at android.os.Looper.loop(Looper.java:123)
07-30 15:17:39.798: E/AndroidRuntime(495):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-30 15:17:39.798: E/AndroidRuntime(495):  at java.lang.reflect.Method.invokeNative(Native Method)
07-30 15:17:39.798: E/AndroidRuntime(495):  at java.lang.reflect.Method.invoke(Method.java:521)
07-30 15:17:39.798: E/AndroidRuntime(495):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-30 15:17:39.798: E/AndroidRuntime(495):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-30 15:17:39.798: E/AndroidRuntime(495):  at dalvik.system.NativeStart.main(Native Method)

第 11 行会发生什么?请告诉我这段代码是什么意思 binary.append((val & 128) == 0 ? 0 : 1)

非常感谢您的帮助... :)

4

3 回答 3

1

Converter类1中的错误:“str”和“hasil”隐藏了超类变量的实例2:它们没有被初始化在MainActivity 1中:“hasil”不是静态的

这是工作代码

public class MainActivity extends Activity{
Converter converter;
static EditText str;
static TextView hasil;
Button convert;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    str = (EditText)findViewById(R.id.et_string);
    hasil = (TextView)findViewById(R.id.txt_hasil);
    convert = (Button)findViewById(R.id.btn_conv);
    convert.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            converter = new Converter();
            converter.aksi1();
        }
    });
}
public class Converter extends MainActivity {
    String kata;
    public void aksi1(){
            kata = str.getText().toString();

            if (kata != null){

            byte[] bytes = kata.getBytes();
            StringBuilder binary = new StringBuilder();
            for(byte b : bytes){
                int val = b;
                for (int i = 0; i < 8; i++){
                    binary.append((val & 128) == 0 ? 0 : 1);// i dont know what this mean
                    val <<= 1;
                }
            }
            hasil.setText(""+binary.toString());
        }
        else{
            convert.setEnabled(false);
        }
    }
}

}

于 2013-07-30T09:01:38.210 回答
0

我在代码中看到的一个问题TextView hasil是从未分配过对象。hasil然后在代码中稍后使用该对象。

public class Converter extends MainActivity {
    EditText str;

    TextView hasil;  //Never assigned so hasil == null

    String kata = str.getText().toString();
    public void aksi1(){
            if (kata != null){

            byte[] bytes = kata.getBytes();
            StringBuilder binary = new StringBuilder();
            for(byte b : bytes){
                int val = b;
                for (int i = 0; i < 8; i++){
                    binary.append((val & 128) == 0 ? 0 : 1);// i dont know what this mean
                    val <<= 1;
                }
            }

            hasil.setText(binary); //Here hasil is being used but is null

        }
        else{
            convert.setEnabled(false);
        }
}
}
于 2013-07-30T08:38:27.420 回答
0

您没有初始化转换器实例中的任何字段,为此创建一个参数化构造函数并使用它来创建转换器的实例,例如:

public class Converter{
    EditText str;
    TextView hasil;

public Converter(EditText str, TextView hasil){
this.str = str;
this.hasil = hasil;
}
// DO OTHER STUFF...
}
于 2013-07-30T08:38:43.327 回答