繁体   English   中英

Android对象未被线程锁定

[英]Android Object not locked by thread

我在我的应用程序中做了一个功能,可以从数据库中删除记录。 我正在使用单独的活动来进行删除。 当我输入要删除的行ID时,我的应用程序会告诉我该应用程序已停止运行,但是当我单击“确定”时,它将返回到主活动屏幕。 我正在使用notifyAll()函数更新数据库和主要活动上的列表视图,我认为这是出问题的地方,但由于我对android还是很陌生,所以我无法弄清原因。

这是我使用notifyAll()的活动

package com.example.rory.dbtest;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.pinchtapzoom.R;


public class Delete extends Activity implements View.OnClickListener{

public EditText edit1;
Button delete;

DBAdapter db = new DBAdapter(this);


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_delete);

    edit1 = (EditText)findViewById(R.id.edit1);
    delete = (Button)findViewById(R.id.deleteBtn);

    delete.setOnClickListener((View.OnClickListener) this);

    }


public void onClick(View v) {

    String userInput = edit1.getText().toString();

    //if the ROW ID its a number (long)
    try{
        long rowID = Long.parseLong(userInput);
        //call the delete method
        db.open();
        db.deleteContact(rowID);
        db.close();
        db.notifyAll();
    }catch(NumberFormatException e){
        Log.e("INPUT ERROR","Input is not a number!",e);
        //notify the user that the input is invalid.
        Toast.makeText(this,"Invalid Value!",Toast.LENGTH_LONG).show();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.delete, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

这是我得到的logcat错误

object not locked by thread before notifyAll()

试试吧:

    synchronized (this){

    }

您需要在并发请求中锁定或同步您的方法。

db.notifyAll();

notifyAllObject的方法,用于唤醒所有正在此对象的监视器上等待的线程。 它不是与sqlite相关的方法,您不需要调用它(因为您的db对象没有充当同步对象)。 您可以摆脱该呼叫。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM