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();
}
};
}
Check out my post which explains the reasons behind this: http://www.levinotik.com/2012/01/10/loopers-handlers-runtimeexceptions-explained/ In short, you need to provide the Handler with a Looper from some thread. You can always get this by doing
new Handler(Looper.getMainLooper()):Also, you should not be managing your own threads inside of Activities, or at least not usually. What you need to do is create the Handler as I showed and then call mHandler.post(runnable), passing in the runnable you want to run. You are needlessly creating a new Thread just so you can call start on it so the Runnable’s run() method is executed. Why not just have it run directly by having the handler post that runnable?
So it should look like:
In fact, if you do it this way, I’d be surprised if it didn’t work even WITHOUT using Looper.getMainLooper();
EDIT
I don’t know what SmsSender is, but that is likely what’s causing you to reach for a new Thread. If SmsSender is doing some long-running work, then what you should do is use an AsyncTask to do that work in the background. Then in onPostExecute you can update your UI with whatevers relevant. You seem to be trying to implement the functionality of AsyncTask yourself, albeit unsuccessfully. See why? You’re trying to do some expensive work in the background and then let the UI update with the status by using a Handler. This is precisely what AsyncTask is meant to help you with, except that it implements all of this correctly and makes it easy for you. 🙂