错误线程退出未捕获的异常

时间:2014-08-17 15:52:06

标签: android android-intent android-fragments android-event

代码:

public class All_view extends ListActivity {

// declare class variables
private Runnable viewParts;
private ItemAdapter m_adapter;
Dbhelper mydb =new Dbhelper(this);
ArrayList<Item> abc = new ArrayList<Item>();
Item it=new Item();
String from,to;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Bundle b=getIntent().getExtras();
    from = b.getString("form");
    to = b.getString("to");
    int flag = b.getInt("flag");
    // instantiate our ItemAdapter class
    m_adapter = new ItemAdapter(this, R.layout.list_item, abc);
     from = b.getString("form");
        to = b.getString("to");
    setListAdapter(m_adapter);

    // here we are defining our runnable thread.
    viewParts = new Runnable(){
        public void run(){
            handler.sendEmptyMessage(0);
        }
    };

    // here we call the thread we just defined - it is sent to the handler below.
    Thread thread =  new Thread(null, viewParts, "MagentoBackground");
    thread.start();
}

private Handler handler = new Handler()
 {
    public void handleMessage(Message msg)
    {
        // create some objects
        // here is where you could also request data from a server
        // and then create objects from that data.
        abc = mydb.getsdata(from,to);
        addptr();

    }
};

public void addptr()
{

    m_adapter = new ItemAdapter(All_view.this, R.layout.list_item, abc);
    // display the list.
    setListAdapter(m_adapter);
}

protected void onListItemClick (ListView arg0, View arg1, int arg2,
        long arg3)
{
    Item i = abc.get(arg2);     
    Toast.makeText(getApplicationContext(), "Please Enter Expenses Type :" +i.getId(),Toast.LENGTH_LONG).show();
    Bundle dataBundle = new Bundle();
    dataBundle.putInt("id", i.getId());
    Intent intent = new Intent(getApplicationContext(),In_add.class);
    intent.putExtras(dataBundle);
    startActivity(intent);        
}

}

错误:

08-17 11:48:20.098: W/dalvikvm(17192): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
08-17 11:48:20.478: E/AndroidRuntime(17192): FATAL EXCEPTION: main
08-17 11:48:20.478: E/AndroidRuntime(17192): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myexpenses/com.example.myexpenses.All_view}: java.lang.NullPointerException
08-17 11:48:20.478: E/AndroidRuntime(17192):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
08-17 11:48:20.478: E/AndroidRuntime(17192):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-17 11:48:20.478: E/AndroidRuntime(17192):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-17 11:48:20.478: E/AndroidRuntime(17192):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-17 11:48:20.478: E/AndroidRuntime(17192):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-17 11:48:20.478: E/AndroidRuntime(17192):    at android.os.Looper.loop(Looper.java:137)
08-17 11:48:20.478: E/AndroidRuntime(17192):    at android.app.ActivityThread.main(ActivityThread.java:5103)
08-17 11:48:20.478: E/AndroidRuntime(17192):    at java.lang.reflect.Method.invokeNative(Native Method)
08-17 11:48:20.478: E/AndroidRuntime(17192):    at java.lang.reflect.Method.invoke(Method.java:525)
08-17 11:48:20.478: E/AndroidRuntime(17192):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-17 11:48:20.478: E/AndroidRuntime(17192):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-17 11:48:20.478: E/AndroidRuntime(17192):    at dalvik.system.NativeStart.main(Native Method)
08-17 11:48:20.478: E/AndroidRuntime(17192): Caused by: java.lang.NullPointerException
08-17 11:48:20.478: E/AndroidRuntime(17192):    at com.example.myexpenses.All_view.<init>(All_view.java:34)
08-17 11:48:20.478: E/AndroidRuntime(17192):    at java.lang.Class.newInstanceImpl(Native Method)
08-17 11:48:20.478: E/AndroidRuntime(17192):    at java.lang.Class.newInstance(Class.java:1130)
08-17 11:48:20.478: E/AndroidRuntime(17192):    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
08-17 11:48:20.478: E/AndroidRuntime(17192):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
08-17 11:48:20.478: E/AndroidRuntime(17192):    ... 11 more

1 个答案:

答案 0 :(得分:0)

如果在没有通过调用putExtras()创建的意图的情况下启动Activity,则Intent.getExtras()可以返回null。

检查空状态并使用默认值。

if (null != intent.getExtras()) {
  Bundle b=getIntent().getExtras();
    from = b.getString("form");
    to = b.getString("to");
    flag = b.getInt("flag");
} else {
   // default values for from to and flag.
}