NullPointerException:lock == null

时间:2013-03-14 18:48:31

标签: android multithreading file

我在活动中遇到问题:我想要读取前一个活动中正确编写的Ordinazioni.txt中的所有文件,将它们放在ArrayList中并将其中的每个元素写入MySQL数据库。

现在,PHP文件和文件是正确的,我已经单独检查了它们的工作,但此时LogCat阻止了应用程序。所以,在这里你是活动和LogCat。

public class AggiungiProdotto extends Activity {

    TextView tv1;
    private static String indirizzo ="http://10.0.2.2/tesina/Aggiungi_Ordinazione";
    FileInputStream in = null;
    InputStreamReader inputStreamReader = new InputStreamReader(in);
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(indirizzo);
    StringBuilder sb = new StringBuilder();
    int Tavolo = 1;
    String line;
    public ArrayList<String> Lettura = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aggiungi_prodotto);
        Invio();
    }

    public void Invio() {
        try {
            FileInputStream in = openFileInput("Ordinazioni.txt");
            while ((line = bufferedReader.readLine()) != null) {
                Lettura.add(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Thread thread = new Thread() {

            @Override
            public void run() {
                try {
                    for (int i = 0; i < Lettura.size(); i++) {
                        List<NameValuePair> Comanda = new ArrayList<NameValuePair>(2);
                        Comanda.add(new BasicNameValuePair("Nome", Lettura.get(i)));
                        Comanda.add(new BasicNameValuePair("Tavolo",
                                Integer.toString(Tavolo).trim()));

                        httppost.setEntity(new UrlEncodedFormEntity(Comanda));

                        ResponseHandler<String> responseHandler =
                                new BasicResponseHandler();
                        final String Risposta = httpclient.execute(httppost,
                                responseHandler);
                        tv1 = (TextView) findViewById(R.id.tv1);
                        tv1.setText("Response : " + Risposta);
                    }
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };

        thread.start();
    }
}

LogCat错误:

03-14 18:40:42.028: E/AndroidRuntime(772): FATAL EXCEPTION: main
03-14 18:40:42.028: E/AndroidRuntime(772): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.gabriele.tesina/com.gabriele.tesina.AggiungiProdotto}: java.lang.NullPointerException: lock == null
03-14 18:40:42.028: E/AndroidRuntime(772):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
03-14 18:40:42.028: E/AndroidRuntime(772):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-14 18:40:42.028: E/AndroidRuntime(772):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-14 18:40:42.028: E/AndroidRuntime(772):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-14 18:40:42.028: E/AndroidRuntime(772):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 18:40:42.028: E/AndroidRuntime(772):  at android.os.Looper.loop(Looper.java:137)
03-14 18:40:42.028: E/AndroidRuntime(772):  at android.app.ActivityThread.main(ActivityThread.java:5039)
03-14 18:40:42.028: E/AndroidRuntime(772):  at java.lang.reflect.Method.invokeNative(Native Method)
03-14 18:40:42.028: E/AndroidRuntime(772):  at java.lang.reflect.Method.invoke(Method.java:511)
03-14 18:40:42.028: E/AndroidRuntime(772):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-14 18:40:42.028: E/AndroidRuntime(772):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-14 18:40:42.028: E/AndroidRuntime(772):  at dalvik.system.NativeStart.main(Native Method)
03-14 18:40:42.028: E/AndroidRuntime(772): Caused by: java.lang.NullPointerException: lock == null
03-14 18:40:42.028: E/AndroidRuntime(772):  at java.io.Reader.<init>(Reader.java:64)
03-14 18:40:42.028: E/AndroidRuntime(772):  at java.io.InputStreamReader.<init>(InputStreamReader.java:122)
03-14 18:40:42.028: E/AndroidRuntime(772):  at java.io.InputStreamReader.<init>(InputStreamReader.java:59)
03-14 18:40:42.028: E/AndroidRuntime(772):  at com.gabriele.tesina.AggiungiProdotto.<init>(AggiungiProdotto.java:32)
03-14 18:40:42.028: E/AndroidRuntime(772):  at java.lang.Class.newInstanceImpl(Native Method)
03-14 18:40:42.028: E/AndroidRuntime(772):  at java.lang.Class.newInstance(Class.java:1319)
03-14 18:40:42.028: E/AndroidRuntime(772):  at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-14 18:40:42.028: E/AndroidRuntime(772):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)

我做错了什么?

3 个答案:

答案 0 :(得分:11)

FileInputStream in = null;
InputStreamReader inputStreamReader = new InputStreamReader(in);

我认为这是一个问题:您将 FileInputStream NULL添加到 InputStreamReader

此外,您的代码更麻烦。看这里

tv1 = (TextView) findViewById(R.id.tv1);
tv1.setText("Response : " + Risposta);

您正在尝试从工作线程更新 UI ,因此如果您要解决第一个问题,则下一个问题就是这个问题。

您无法使用工作线程的UI进行操作。这不被允许。只有始发(UI)线程才能使用UI进行操作。

因此,如果你想让它工作,你需要使用已经在UI线程而不是普通线程上运行的runOnUiThread()

注意:为了改进您的代码,我建议您使用AsyncTask执行Internet操作,并在某些操作期间/之后执行UI更新。 AsyncTask被指定用于像你这样的东西。如果你以前从未实现它,那么这里是nice tutorial by Lars Vogel

答案 1 :(得分:2)

除了指出的其他问题之外,请注意您尝试从新线程更新TextView。 Android的UI系统不是线程安全的,因此以下行是另一个问题:

tv1.setText("Response : " + Risposta);'

您可以使用this example from the docs解决此问题,而无需对代码进行过多重组。它使用Handler类返回UI线程。

答案 2 :(得分:0)

我收到了类似的错误。我遇到的问题是,我在Mac上运行了两个Android Studio实例,当我在设备上运行应用程序时,它们彼此冲突。

关闭Android Studio的一个实例后,我能够毫无问题地运行应用程序。