java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序为什么?

时间:2013-12-10 12:06:04

标签: java android multithreading

当我运行第一个活动时......没有错误 但是,当我尝试运行第二个活动ERROR时 “java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序”

没有循环问题的活动

package com.example.androidnotizer;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class LoginActivity extends Activity {

public static final String URL="jdbc:mysql://10.0.2.2/notizer";
public static final String USER="root";
public static final String PASSWORD="apple";
public static final String Driver_Class="com.mysql.jdbc.Driver";
public static final int RED=-65536;

Handler handler = new Handler();
Thread background;
String uid,pass;
int count=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    TextView tv = (TextView)findViewById(R.id.textView4);
    tv.setText("");
    Button login = (Button) findViewById(R.id.button1);
    login.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View arg0) {
            EditText et1 = (EditText)findViewById(R.id.editText1);
            EditText et2 = (EditText)findViewById(R.id.editText2);
            uid=et1.getText().toString();
            pass=et2.getText().toString();
            TextView tv = (TextView)findViewById(R.id.textView4);
            tv.setText("");
            count=0;
            doLogin();
        }
    });

}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_signup:
            Intent i = new Intent(LoginActivity.this,SignUpActivity.class);
            startActivity(i);
    }
    return true;
}

public void doLogin(){
    background = new Thread(new Runnable(){
        @Override
        public void run(){
            try{
                Class.forName(Driver_Class);
                Connection con = DriverManager.getConnection(URL, USER, PASSWORD);
                Statement st = con.createStatement();
                ResultSet rs =st.executeQuery("select * from login where user_id='"+ uid +"' and password='"+ pass +"'");
                while(rs.next()){
                    count++;
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            Runnable r=new Runnable() {
                   @Override
                   public void run() {
                       if(count==0){
                           Toast.makeText(getApplicationContext(), "Unsuccessful Login", Toast.LENGTH_SHORT).show();
                            TextView tv = (TextView)findViewById(R.id.textView4);
                            tv.setText("Wrong user_id or password");
                            tv.setTextColor(RED);  
                       }
                       else{
                           Intent i = new Intent(LoginActivity.this,NoticeActivity.class);
                           startActivity(i);
                       }
                   }
                };
                handler.post(r);
            }
        }
    );
    background.start();
}

}

活跃与循环问题

package com.example.androidnotizer;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class StudentSignUpActivity extends Activity implements OnClickListener {

public static final String URL="jdbc:mysql://10.0.2.2/notizer";
public static final String USER="root";
public static final String PASSWORD="apple";
public static final String Driver_Class="com.mysql.jdbc.Driver";

Handler handler = new Handler();
Thread background;

Button button,combi;
ListView listView;
ArrayAdapter<String> adapter;
String fname,lname,mname,gender,mobile,designation,passPapers,roll,department,year;
int count=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_student_sign_up);
    Intent intent =getIntent();
    fname = intent.getStringExtra("fname");
    mname = intent.getStringExtra("mname");
    lname = intent.getStringExtra("lname");
    gender = intent.getStringExtra("gender");
    mobile = intent.getStringExtra("mobile");
    designation = intent.getStringExtra("designation");
    roll = intent.getStringExtra("roll");
    department = intent.getStringExtra("department");
    year = intent.getStringExtra("year");
    findViewById();
    String[] sports = getResources().getStringArray(R.array.papers_array);
    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice, sports);
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listView.setAdapter(adapter);
    button.setOnClickListener(this);
}

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

private void findViewById() {
    listView = (ListView) findViewById(R.id.dept_listView1);
    button = (Button) findViewById(R.id.done);
}

@Override
public void onClick(View arg0) {
    SparseBooleanArray checked = listView.getCheckedItemPositions();
    passPapers="";
    for (int i = 0; i < checked.size(); i++) {
        int position = checked.keyAt(i);
        if (checked.valueAt(i))
            if((i+1) < checked.size()){
                passPapers=passPapers+String.valueOf(adapter.getItem(position))+",";
            }
            else{
                passPapers=passPapers+String.valueOf(adapter.getItem(position));
            }

    }
    doSignUp();
}

public void doSignUp(){
    Toast.makeText(getApplicationContext(),"Inside doSignUp()",Toast.LENGTH_LONG).show();
    background = new Thread(new Runnable(){
        @Override
        public void run(){
            try{
                Toast.makeText(getApplicationContext(),"Inside try",Toast.LENGTH_LONG).show();
                Class.forName(Driver_Class);
                Connection con = DriverManager.getConnection(URL, USER, PASSWORD);
                Statement st = con.createStatement();
                //st.executeUpdate("insert into student(roll_no,fname,mname,lname,gender,mobile_no,department,year,pass_papers,desigantion) values('"+ roll +"','"+ fname +"','"+ mname +"','"+ lname +"','"+ gender +"','"+ mobile +"','"+ department +"','"+ year +"','"+ passPapers +"','"+ designation +"')");
                st.executeUpdate("insert into login(user_id,password) values('"+ roll +"','"+ roll +"')");
                count=1;
            }catch(Exception e){
                count=0;
                e.printStackTrace();
                Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
            }
            Runnable r=new Runnable() {
                   @Override
                   public void run() {
                       if(count==1){
                           Toast.makeText(getApplicationContext(), "Successful Sign Up\nYour user_id : " + roll + "\nand password : " + roll, Toast.LENGTH_LONG).show();
                           Intent i = new Intent(StudentSignUpActivity.this,NoticeActivity.class);
                           startActivity(i);
                       }
                       else{
                           Toast.makeText(getApplicationContext(), "Unsuccessful Sign Up\nTry Again", Toast.LENGTH_LONG).show();
                           Intent i = new Intent(StudentSignUpActivity.this,SignUpActivity.class);
                           startActivity(i);
                       }
                   }
                };
                handler.post(r);
            }
        }
    );
    background.start();
}

}

logcat的

12-10 06:49:48.823: E/AndroidRuntime(5021): FATAL EXCEPTION: Thread-344
12-10 06:49:48.823: E/AndroidRuntime(5021): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
12-10 06:49:48.823: E/AndroidRuntime(5021):     at android.os.Handler.<init>(Handler.java:197)
12-10 06:49:48.823: E/AndroidRuntime(5021):     at android.os.Handler.<init>(Handler.java:111)
12-10 06:49:48.823: E/AndroidRuntime(5021):     at android.widget.Toast$TN.<init>(Toast.java:324)
12-10 06:49:48.823: E/AndroidRuntime(5021):     at android.widget.Toast.<init>(Toast.java:91)
12-10 06:49:48.823: E/AndroidRuntime(5021):     at android.widget.Toast.makeText(Toast.java:238)
12-10 06:49:48.823: E/AndroidRuntime(5021):     at com.example.androidnotizer.StudentSignUpActivity$1.run(StudentSignUpActivity.java:104)
12-10 06:49:48.823: E/AndroidRuntime(5021):     at java.lang.Thread.run(Thread.java:841)

2 个答案:

答案 0 :(得分:2)

您必须从UI线程本身运行T​​oast消息。使用此:

LoginActivity.this.runOnUiThread(new Runnable() {
        public void run() {

         if(count==1){
                       Toast.makeText(getApplicationContext(), "Successful Sign Up\nYour user_id : " + roll + "\nand password : " + roll, Toast.LENGTH_LONG).show();
                       Intent i = new Intent(StudentSignUpActivity.this,NoticeActivity.class);
                       startActivity(i);
                   }
         else{
                       Toast.makeText(getApplicationContext(), "Unsuccessful Sign Up\nTry Again", Toast.LENGTH_LONG).show();
                       Intent i = new Intent(StudentSignUpActivity.this,SignUpActivity.class);
                       startActivity(i);
                   }
                        });

将此放在您创建的主题中。

答案 1 :(得分:0)

public void doSignUp(){
        background = new Thread(new Runnable() {
            @Override
            public void run() {
                String error = "-1";
                try {
                    Class.forName(Driver_Class);
                    Connection con = DriverManager.getConnection(URL, USER, PASSWORD);
                    Statement st = con.createStatement();
                    st.executeUpdate("insert into login(user_id,password) values('"+ roll +"','"+ roll +"')");
                    count=1;
                } catch(Exception e) {
                    count=0;
                    e.printStackTrace();
                    error = e.toString();
                }
                final String cause = error;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(count==1) {
                            Toast.makeText(getApplicationContext(), "Successful Sign Up\nYour user_id : " + roll + "\nand password : " + roll, Toast.LENGTH_LONG).show();
                            Intent i = new Intent(StudentSignUpActivity.this,NoticeActivity.class);
                            startActivity(i);
                        } else {
                            Toast.makeText(getApplicationContext(), "Unsuccessful Sign Up\nTry Again (Cause : " + cause + ")", Toast.LENGTH_LONG).show();
                            Intent i = new Intent(StudentSignUpActivity.this,SignUpActivity.class);
                            startActivity(i);
                        }
                    }
                });
            }
        });
        background.start();
    }

试试这个样本