着创造处理器内螺纹已经不叫尺蠖prepare尺蠖、不叫、处理器、内螺纹

2023-09-05 03:24:18 作者:满满负能量的 负能量爆棚的昵称

我是新手的Andr​​oid程序员。只是看到了一些网上的教程开始,我决定做一个应用程序为我自己..这是工作的罚款,直到昨天。我今天所做的唯一变化就是加入这一行this.requestWindowFeature(Window.FEATURE_NO_TITLE);并指出引发错误。请提供此溶液

 公共类MainActivity扩展活动实现OnClickListener,可运行{
上下文语境;

的EditText editTextNum,EDITTEXT,editUserName,editPassword;
按钮btnsend;
ProgressDialog PD;

@覆盖
公共无效的onCreate(包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 =(按钮)findViewById(R.id.btnsend);
    btnsend.setOnClickListener(本);
}

/ **当用户点击发送按钮调用* /
公共无效的sendMessage(){
    如果(!isOnline()){
        Toast.makeText(MainActivity.this,没有互联网Access..Cannot发送短信,Toast.LENGTH_LONG).show();
    } 其他 {
        字符串username = editUserName.getText()的toString()。
        。字符串password = editPassword.getText()的toString();
        。弦数= editTextNum.getText()的toString();
        字符串消息= editText.getText()的toString()。
        字符串msgreciever =号;
        字符串的TestMessage =消息;
        尝试 {
            SmsSender.sendMessage(msgreciever,的TestMessage,用户名,密码);
        }赶上(例外前){
            Toast.makeText(MainActivity.this,短信发送失败,Toast.LENGTH_LONG).show();
        }

    }
}

@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    。getMenuInflater()膨胀(R.menu.activity_main,菜单);
    返回true;
}

公共布尔isOnline(){
    ConnectivityManager厘米=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    的NetworkInfo的NetInfo = cm.getActiveNetworkInfo();
    如果(NetInfo的= NULL和放大器;!&安培; netInfo.isConnectedOrConnecting()){
        返回true;
    }
    返回false;
}

公共无效的onClick(视图v){
    // TODO自动生成方法存根
    如果(V == btnsend){
        如果(!isOnline()){
            Toast.makeText(MainActivity.this,没有互联网Access..Cannot发送短信,Toast.LENGTH_LONG).show();
        } 其他 {
            PD = ProgressDialog.show(MainActivity.this,免费短信,发送SMS..Please等等.. !!,真正的);
            线程t =新主题(本);
            t.start();
        }

    }
}

公共无效的run(){
    // TODO自动生成方法存根
    发信息();
    mHandler.sendEmptyMessage(0);
}

公开处理程序mHandler =新的处理程序(){
    @覆盖
    公共无效的handleMessage(信息MSG){
        // TODO自动生成方法存根
        super.handleMessage(MSG);
        pd.dismiss();
        Toast.makeText(MainActivity.this,邮件发送成功,Toast.LENGTH_LONG).show();
        editTextNum.setText();
        editText.setText();
        editTextNum.requestFocus();
    }
};
}
 

解决方案

看看我的帖子这也解释了这背后的原因:的 http://www.levinotik.com/2012/01/10/loopers-handlers-runtimeexceptions-explained/ 总之,你需要提供处理器从某个线程一个活套。你总是可以得到这样做新的处理程序(Looper.getMainLooper()):

此外,你不应该管理自己的线程活动的内部,或至少不一般。什么,你需要做的是建立处理程序,我发现,然后调用mHandler.post(可运行的),传递你想要运行的可运行。你是不必要的创建一个新的线程,所以您可以拨打开始就这么被执行Runnable的run()方法。为什么不把它直接通过让处理器运行后的可运行?

因此​​,它应该是这样的:

 处理程序mHandler =新的处理程序(Looper.getMainLooper());

可运行myRunnable =新的Runnable(){
 公共无效的run(){在这里工作......}
}

...别处

mHandler.post(myRunnable);
 

事实上,如果你做这种方式,我会感到惊讶,如果它没有即使不使用工作Looper.getMainLooper();

修改

我不知道是什么SmsSender是,这很可能是什么导致你达到一个新的主题。如果SmsSender是做一些长时间运行的工作,那么你应该做的是使用的AsyncTask来做这项工作的背景。然后,在onPostExecute您可以更新你的用户界面与相关凡是。你似乎在试图实现的AsyncTask自己的功能,虽然不成功。知道为什么吗?你试图做一些费时的工作在后台,然后让有状态的UI更新通过使用一个处理程序。这是precisely什么的AsyncTask是为了帮助你,但它实现了这一切正确,很容易让你。 :)

我国历史上最变态的五种兵器,最后一种曾让倭寇吃尽了苦头

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:

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

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

...elsewhere

mHandler.post(myRunnable);

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. :)