2

I am newbie android programmer. just started by seeing some online tutorials, i decided to make a application for myself.. it was working fine till yesterday.. The only change i made today was to add this line "this.requestWindowFeature(Window.FEATURE_NO_TITLE);" and it stated throwing the error. pls provide the solution for this.

public class MainActivity extends Activity implements OnClickListener, Runnable {
Context context;

EditText editTextNum, editText, editUserName, editPassword;
Button btnsend;
ProgressDialog pd;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);     
    editUserName = (EditText) findViewById(R.id.edit_userName);
    editPassword = (EditText) findViewById(R.id.edit_password);
    editTextNum = (EditText) findViewById(R.id.edit_number);
    editText = (EditText) findViewById(R.id.edit_message);      
    btnsend = (Button) findViewById(R.id.btnsend);
    btnsend.setOnClickListener(this);
}

/** Called when the user clicks the Send button */
public void sendMessage() {
    if (!isOnline()) {
        Toast.makeText(MainActivity.this,"No Internet Access..Cannot Send SMS", Toast.LENGTH_LONG).show();
    } else {
        String userName = editUserName.getText().toString();
        String password = editPassword.getText().toString();
        String number = editTextNum.getText().toString();
        String message = editText.getText().toString();
        String msgreciever = number;
        String testMessage = message;
        try {
            SmsSender.sendMessage(msgreciever, testMessage, userName, password);
        } catch (Exception ex) {
            Toast.makeText(MainActivity.this, "SMS Sending Failed.",Toast.LENGTH_LONG).show();
        }

    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v == btnsend) {
        if (!isOnline()) {
            Toast.makeText(MainActivity.this,"No Internet Access..Cannot Send SMS",Toast.LENGTH_LONG).show();
        } else {
            pd = ProgressDialog.show(MainActivity.this, "Free Sms","Sending SMS..Please Wait..!!", true);
            Thread t = new Thread(this);
            t.start();
        }

    }
}

public void run() {
    // TODO Auto-generated method stub
    sendMessage();
    mHandler.sendEmptyMessage(0);
}

public Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        super.handleMessage(msg);
        pd.dismiss();
        Toast.makeText(MainActivity.this, "Message Sent Successfully",Toast.LENGTH_LONG).show();
        editTextNum.setText("");
        editText.setText("");
        editTextNum.requestFocus();
    }
};
}
4

2 回答 2

4

查看我的帖子,其中解释了这背后的原因:http: //www.levinotik.com/2012/01/10/loopers-handlers-runtimeexceptions-explained/简而言之,您需要为处理程序提供来自某个线程的 Looper . 你总是可以通过这样做得到这个new Handler(Looper.getMainLooper()):

此外,您不应该在活动中管理自己的线程,或者至少不应该经常这样做。你需要做的是创建我展示的 Handler,然后调用 mHandler.post(runnable),传入你想要运行的 runnable。您不必要地创建一个新线程,以便您可以在其上调用 start 以便执行 Runnable 的 run() 方法。为什么不直接通过让处理程序发布可运行的来直接运行它呢?

所以它应该看起来像:

Handler mHandler = new Handler(Looper.getMainLooper());

Runnable myRunnable = new Runnable() {
 public void run() {work here...}
}

...elsewhere

mHandler.post(myRunnable);

事实上,如果你这样做,即使不使用 Looper.getMainLooper(); 它也不起作用,我会感到惊讶。

编辑

我不知道 SmsSender 是什么,但这很可能是导致您访问新线程的原因。如果 SmsSender 正在做一些长时间运行的工作,那么您应该做的是使用 AsyncTask 在后台完成该工作。然后在 onPostExecute 中,您可以使用相关的任何内容更新您的 UI。您似乎正在尝试自己实现 AsyncTask 的功能,尽管没有成功。看看为什么?您正在尝试在后台做一些昂贵的工作,然后通过使用处理程序让 UI 更新状态。这正是 AsyncTask 旨在为您提供帮助的目的,只是它正确地实现了所有这些并让您轻松。:)

于 2012-09-02T04:40:54.483 回答
0

这对我有用,但我使用的是 ProgressDialog。

myProgressDialog = ProgressDialog.show(MyActivity.this,
                getString(R.string.texto1) + "...",
                getString(R.string.texto2) + "...", true);
        myProgressDialog.getWindow().setType(
                WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
        new Thread( new Runnable() {     
            public void run() {     
                runOnUiThread(new Runnable(){
                    public void run() { 

                        Toast t = Toast.makeText(getApplicationContext(), "cadena a mostrar", Toast.LENGTH_SHORT);
                        t.show();

                    }


                });
                myProgressDialog.dismiss();
            }                         
        }).start();
于 2013-03-20T13:05:22.137 回答