开发者

convert String to CharSequence or char[] in Java (Android 1.6 API)

i've got a String that i need to convert to a CharSequence. the data MUST necessarily be a String, as it's returned from:

String ip = pref.getString("ip", "0.0.0.0");

which grabs a user preference String. the data needs to be passed to any of these functions:

final void setText(int resid)
final void setText(int resid, TextView.BufferType type)
final void setText(char[] text, int start, int len)
void setText(CharSequence text, TextView.BufferType type)
final void setText(CharSequence text)

i tried using the bottom function first, but you can't pass it a String (even though String implements CharSequence). it'll compile but it crashes. casting it to a CharSequence in the function call like this doesn't work either:

ipIn.setText((CharSequence)ip);

then i tried using the array version by calling:

ipIn.setText(ip.toCharArray(), 0, ip.toCharArray().length-1);

still crashes. here's the output from 'adb logcat':

E/AndroidRuntime(16642): FATAL EXCEPTION: main

E/AndroidRuntime(16642): java.lang.NullPointerException

E/AndroidRuntime(16642):    at com.conceptualsystems.android4api.sms.smsMobile.onCreateDialog(smsMobile.java:81)

E/AndroidRuntime(16642):    at android.app.Activity.onCreateDialog(Activity.java:2472)

E/AndroidRuntime(16642):    at android.app.Activity.createDialog(Activity.java:881)

E/AndroidRuntime(16642):    at android.app.Activity.showDialog(Activity.java:2547)

E/AndroidRuntime(16642):    at android.app.Activity.showDialog(Activity.java:2514)

E/AndroidRuntime(16642):    at com.conceptualsystems.android4api.sms.smsMobile.onOptionsItemSelected(smsMobile.java:120)

E/AndroidRuntime(16642):    at android.app.Activity.onMenuItemSelected(Activity.java:2195)

E/AndroidRuntime(16642):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)

E/AndroidRuntime(16642):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)

E/AndroidRuntime(16642):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:开发者_如何学Go855)

E/AndroidRuntime(16642):    at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)

E/AndroidRuntime(16642):    at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)

E/AndroidRuntime(16642):    at android.view.View$PerformClick.run(View.java:8816)

E/AndroidRuntime(16642):    at android.os.Handler.handleCallback(Handler.java:587)

E/AndroidRuntime(16642):    at android.os.Handler.dispatchMessage(Handler.java:92)

E/AndroidRuntime(16642):    at android.os.Looper.loop(Looper.java:123)

E/AndroidRuntime(16642):    at android.app.ActivityThread.main(ActivityThread.java:4627)

E/AndroidRuntime(16642):    at java.lang.reflect.Method.invokeNative(Native Method)

E/AndroidRuntime(16642):    at java.lang.reflect.Method.invoke(Method.java:521)

E/AndroidRuntime(16642):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

E/AndroidRuntime(16642):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

E/AndroidRuntime(16642):    at dalvik.system.NativeStart.main(Native Method)

W/ActivityManager( 1099):   Force finishing activity

what's interesting is that it crashes when AlertDialog.Builder.setView(View) is called if i set the text first, but crashes on setText if i call it after setView. here's some code to put things into context:

final View dialogLayout = inflater.inflate(R.layout.dialog_wifi_pref, null);
builder = new AlertDialog.Builder(this);
builder.setView(dialogLayout);
builder.setTitle(R.string.opt_ip_config);
String ip = pref.getString("ip", "0.0.0.0");
String port = pref.getString("port", "3005");
EditText ipIn = (EditText)findViewById(R.id.wifi_ip_in);
EditText portIn = (EditText)findViewById(R.id.wifi_port_in);
//ipIn.setText(ip.toCharArray(), 0, ip.toCharArray().length-1);
//portIn.setText((CharSequence)port.toString());
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        // change to sqlite /////
        /////////////////////////
        //nothing here yet
        dialog.dismiss();
    }
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        dialog.cancel();
    }
});
dialog = builder.create();

this is all part of the onCreateDialog override and exists inside a case statement. 'dialog', 'inflater', and 'builder' are defined above (properly). and like i said, the error changes depending on whether call setView or setText first. i'm not sure which one i'm actually supposed to do first.

the bottom line: i have to get a String into this EditText box. how does i?


Try running this through a debugger. You are probably passing a null reference into your method, thus the NullPointerException. That would explain why the code compiles but fails at runtime.


Im not sure about your code.but if you have problem in converting. Best way will be to try to make this conversion in a working sample in activity like:

public class test extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try{
        //Here do your converting operation
        //
        }catch(Exception e){
            //show any error during converting
        }
        android.util.Log.v("conv",
         //here you can show results in logcat 
         );

    }
}

if you get expected results , check your dialog with fixed data. final step mix logic (conversion) + View (dialog) .. and you will be happy .. ;)


The most likely explanation is that the text String you are setting is null. The second alternative does ip.toCharArray() which will definitely throw an NPE if ip is null. The first alternative most likely will too, though the NPE may happen a bit later.

In other circumstances, you might get this kind of problem if you called EditText.setText() in the wrong thread. But that shouldn't be the problem here since you are in the process of creating the widget.


fixed.

the problem actually had nothing at all to do with the class casting. the problem was in the way i was referring to the dialog layout's views. i replaced this line:

EditText ipIn = (EditText)findViewById(R.id.wifi_ip_in);

with this line:

EditText ipIn = (EditText)dialogLayout.findViewById(R.id.wifi_ip_in);

i guess when you inflate a dialog, you have to refer to the inflated instance explicitly, otherwise android won't know which instance of the dialog to change? so the null pointer was actually the dialog EditText ipIn not getting set properly.

thanks to all who contributed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜